4

There are quite a few places to suggest using ForeignKey for one-to-many or many-to-one relationship, but I am struggling to understand how this will work without reversing dependency.

For example:

I have these 2 apps, 'settlements' and 'payments', ideally, settlements don't need to know about payments because payment records are created after settlements have been made, and a payment record can contain multiple settlements.

settlements model:

class Settlement(models.Model):
    ...

payments model:

class Payment(models.Model):
    settlements = models.ManyToManyField('Settlement')
    ....

This brings a fundamental schema caveat, that settlement can belong to multiple payments, which should not happen.

To enforce a settlement cannot belong to more than one payment record, I will have to put the payment foreignkey in Settlement:

settlements model:

class Settlement(models.Model):
    payment = models.ForeignKey('Payment')
    ...

payments model:

class Payment(models.Model):
    ....

While this enforce the schema validity, but seems the dependency has reversed, now that settlement needs to know about payment, just made these 2 apps tightly coupled. Also I need to create a settlement record with payment field being null, create the payment record, then go back to the settlement record to link up the payment record, this seems wrong.

2
  • why the second approach seems to be wrong? conceptually, in real life, you need to create your settlement instance first, then the payment and then bind them.
    – levi
    Sep 3, 2014 at 3:06
  • Perhaps a better way to conceptualize the relationship is as belongs to -- Settlement 1, 3, and 9 belong to Payment 4.
    – DivinusVox
    Sep 3, 2014 at 3:12

2 Answers 2

2

Though the schema may make sense to have a foreign key on Settlement that links to a Payment, your application doesn't have to have that relation (or enforce it on the database). I will assume that Settlement can't be changed, and that you can only work with the Payment app.

Simply create your own intermediate model/table, and link that with payments. It'll be like having a ManyToMany relationship, but you can enforce the constraints on it.

class Payment(models.Model):
   # your fields go here

class Invoice(models.Model):
    settlement = models.OneToOneField('settlements.Settlement')
    payment = models.ForeignKey(Payment)

Note the OneToOneField. That's essentially a Foreign Key with a unique constraint. We're emulating a ManyToMany intermediate table, but restricting the settlement to only having a single record in the invoice table.

I should probably note that because we're creating a new table here, that future queries may be slow due to the extra join required. If you modify the Settlement model to include a ForeignKey, it'll perform and look better from a normalisation sense.

1

A colleague of mine found this project:

https://github.com/adsworth/django-onetomany

It's adding a unique together constraint in the manytomany relationship.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.