Reputation: 3443
I can't add two foreign key constraints to the same field?
I am not 100% sure about the relationship, so I have included the tables. Hope it can help others with learning Django as well: Transaction
+----------+-----+---------------------+
| guid | plu | datetime |
+----------+-----+---------------------+
| 00003516 | 1 | 2015-09-22 12:11:12 |
| 0000386a | 2 | 2015-02-22 12:11:10 |
| 0000c59d | 2 | 2015-03-22 12:11:10 |
| 0000f03f | 3 | 2015-01-22 12:12:12 |
+----------+-----+---------------------+
Plu
+-----+------+
| plu | name |
+-----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+-----+------+
Finance
+-----+------------+------------+-------+
| plu | from | to | price |
+-----+------------+------------+-------+
| 1 | 2013-01-22 | 2015-01-23 | 10 |
| 1 | 2015-01-23 | 2015-02-29 | 20 |
| 2 | 2013-01-22 | 2015-01-22 | 10 |
| 3 | 2013-01-22 | 2015-01-22 | 30 |
+-----+------------+------------+-------+
Based on this I have created the following models: Finance
class Finance(models.Model):
guid = models.AutoField(primary_key=True)
from = models.DateField()
to = models.DateField()
price = models.DecimalField(max_digits=10, decimal_places=2)
PLU
class Plu(models.Model):
plu = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200, null=True)
Transaction
class Transaction(models.Model):
guid = models.CharField(primary_key=True, max_length=38)
plu = models.ForeignKey(Plu, db_column='plu')
finance = models.ForeignKey(Finance, db_column='plu')
datetime = models.DateTimeField()
When I try to run this i get the following error - what to do?:
app.Transaction: (models.E007) Field 'finance' has column name 'plu' that is used by another field.
HINT: Specify a 'db_column' for the field.
Upvotes: 1
Views: 1117
Reputation: 43330
You've copy and pasted and included the same db_column name, you need to change it
finance = models.ForeignKey(Finance, db_column='finance')
Note: You don't actually need the db_column specified at all since you're just setting it to the name of the field anyway
finance = models.ForeignKey(Finance)
Upvotes: 1