我无法将两个外键约束添加到同一个字段?多个ForeignKey到Django中的相同字段
我不是100%确定关系,所以我已经包括了表格。希望它可以帮助别人学习Django的还有: 交易
+----------+-----+---------------------+
| 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 |
+-----+------+
财经
+-----+------------+------------+-------+
| 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 |
+-----+------------+------------+-------+
在此基础上我已经创建了如下型号: 财经
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)
交易
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()
当我试图运行此我得到下面的错误 - 该怎么办?:
app.Transaction: (models.E007) Field 'finance' has column name 'plu' that is used by another field.
HINT: Specify a 'db_column' for the field.
我遇到了完全相同的问题,但我想澄清一下,约束是物理数据库列的关系派生自必须是相同的吗?我有一个只读数据库,并且我不能将'db_column'更改为其他内容,因为那样关系就会中断。 – chuckus