databse design one table for several tables - django models

I’m working on a project has two models one for Invoice and the other for payments, in the both tables should have a field named next pay for the loaners to determine when he/she should pay his/her loan

class Payment(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=20,decimal_places=3)
    #others

class CustomerInvoice(models.Model):
    seller = models.ForeignKey(User,on_delete=models.PROTECT)
    customer = models.CharField(max_length=50)
    #others

should i add new fields named next pay for both two models or create a new model , something like this

class NextPayment(models.Model):
    next_pay = models.DateTimeField()

and add NextPayment as ForeignKey for two both models ? which one is the most effective way please ?
thank you in advance …

Is there a relationship between Payment and CustomerInvoice? Is there a difference in how that next_pay is either determined or identified?

If there’s really no relationship or commonality between those two models, then you’d want to add a next_pay field to each.

1 Like

sorry, the relation between NextPayment with the two other models are OneToOne . no, there is not relations between Payment and CustomerInvoice . no there is no difference . but we have another model named Clients , both Payment and CustomerInvoice have relationships with Client , and its ForeignKey . i need to make it simple as possible to create the query , just return objects from NextPayment , i know i can merge two queries , but i think creating NextPayment will be better ! isnt it ?

There’s not a blanket / universal answer to that question. It’s always a tradeoff between addressing issues like “get me all next_pay dates” and “let’s update the next_pay for a Payment that has been received” or “let’s define the next_pay for this new CustomerInvoice” being generated.

Personally, I always rely upon the fundamental principle of “model the data first, let the code follow”. I map my entities to the real-world objects they’re supposed to model and start from that point. Then, only after determining that a model doesn’t work do I look to alter the model to make the code more performant. I never start from the perspective of "How do I model this to make my queries “easier”.

1 Like

thank you, so ill change it to adding next_pay for each two models

but it will be possible for updating the next_pay field after getting the price ? i added a boolean field to the NextPayment model its True by default, when a new payment created , ill filter Clients with the name that paid , and update the boolean field to false