Django - Python 3 - “AssertionError:一个模型不能有多个 AutoField。”
Posted
技术标签:
【中文标题】Django - Python 3 - “AssertionError:一个模型不能有多个 AutoField。”【英文标题】:Django - Python 3 - "AssertionError: A model can't have more than one AutoField." 【发布时间】:2017-09-14 06:47:49 【问题描述】:我快疯了。
我使用 mysqlWorkbench 创建了我的数据库
This his my Schema
比我使用终端命令获取模型代码:
$python3 manage.py inspectdb
将代码传递给我的 models.py 后,我尝试在 shell 中使用模型
$python3 manage.py shell
但是我总是遇到这个错误:
“AssertionError:一个模型不能有多个 AutoField。”
但是这个错误没有意义,因为每个Model中只有一个AutoField,见:
class Brands(models.Model):
bid = models.AutoField(db_column='BID') # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=45, blank=True, null=True) # Field name made lowercase.
fair = models.IntegerField(db_column='Fair', blank=True, null=True) # Field name made lowercase.
eco = models.IntegerField(db_column='Eco', blank=True, null=True) # Field name made lowercase.
country = models.CharField(db_column='Country', max_length=45, blank=True, null=True) # Field name made lowercase.
companies = models.ForeignKey('Companies', models.DO_NOTHING, db_column='Companies_ID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Brands'
unique_together = (('bid', 'companies'),)
class Companies(models.Model):
cid = models.AutoField(db_column='CID') # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=45, blank=True, null=True) # Field name made lowercase.
fair = models.IntegerField(db_column='Fair', blank=True, null=True) # Field name made lowercase.
eco = models.IntegerField(db_column='Eco', blank=True, null=True) # Field name made lowercase.
country = models.CharField(db_column='Country', max_length=45, blank=True, null=True) # Field name made lowercase.
concerns = models.ForeignKey('Concerns', models.DO_NOTHING, db_column='Concerns_ID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Companies'
unique_together = (('cid', 'concerns'),)
class Concerns(models.Model):
cid = models.AutoField(db_column='CID', primary_key=True) # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=45, blank=True, null=True) # Field name made lowercase.
fair = models.IntegerField(blank=True, null=True)
eco = models.IntegerField(db_column='Eco', blank=True, null=True) # Field name made lowercase.
country = models.CharField(db_column='Country', max_length=45, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'Concerns'
class Products(models.Model):
pid = models.AutoField(db_column='PID') # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=45, blank=True, null=True) # Field name made lowercase.
ean = models.IntegerField(db_column='EAN', blank=True, null=True) # Field name made lowercase.
fair = models.IntegerField(db_column='Fair', blank=True, null=True) # Field name made lowercase.
eco = models.IntegerField(db_column='Eco', blank=True, null=True) # Field name made lowercase.
companies_id = models.IntegerField(db_column='Companies_ID') # Field name made lowercase.
brands = models.ForeignKey(Brands, models.DO_NOTHING, db_column='Brands_ID') # Field name made lowercase.
brands_companies = models.ForeignKey(Brands, models.DO_NOTHING, db_column='Brands_Companies_ID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Products'
unique_together = (('pid', 'companies_id', 'brands', 'brands_companies'),)
class ProductsHasShoppinglists(models.Model):
products = models.ForeignKey(Products, models.DO_NOTHING, db_column='Products_ID') # Field name made lowercase.
products_companies = models.ForeignKey(Products, models.DO_NOTHING, db_column='Products_Companies_ID') # Field name made lowercase.
shoppinglists = models.ForeignKey('Shoppinglists', models.DO_NOTHING, db_column='ShoppingLists_ID') # Field name made lowercase.
shoppinglists_users = models.ForeignKey('Shoppinglists', models.DO_NOTHING, db_column='ShoppingLists_Users_ID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Products_has_ShoppingLists'
unique_together = (('products', 'products_companies', 'shoppinglists', 'shoppinglists_users'),)
class Shoppinglists(models.Model):
id = models.AutoField(db_column='ID') # Field name made lowercase.
products = models.CharField(db_column='Products', max_length=45, blank=True, null=True) # Field name made lowercase.
users = models.ForeignKey('Users', models.DO_NOTHING, db_column='Users_ID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'ShoppingLists'
unique_together = (('id', 'users'),)
class Users(models.Model):
uid = models.AutoField(db_column='UID', primary_key=True) # Field name made lowercase.
firstname = models.CharField(db_column='FirstName', max_length=45, blank=True, null=True) # Field name made lowercase.
lastname = models.CharField(db_column='LastName', max_length=45, blank=True, null=True) # Field name made lowercase.
email = models.CharField(db_column='Email', max_length=45, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'Users'
我就是不明白这个问题!!!
【问题讨论】:
【参考方案1】:来自docs:
默认情况下,Django 为每个模型提供以下字段:
id = models.AutoField(primary_key=True)
这是一个自动递增的主键。
如果您想指定自定义主键,只需指定 primary_key=True 在您的一个字段上。如果 Django 看到你 显式设置 Field.primary_key,它不会添加自动 id 列。
每个模型只需要一个字段来使 primary_key=True(要么 显式声明或自动添加)。
所以尝试像这样设置primary_key=True
:
bid = models.AutoField(db_column='BID', primary_key=True)
【讨论】:
我的问题是我没有意识到自动添加的列也是一个AutoField。感谢您的报价以及提示和提示等! ?【参考方案2】:尝试在您的 AutoField 中设置 primary_key=True。因为如果您不设置 primary_key=True django 将自动创建其他 AutoFiled。请参阅下面的文档:
https://docs.djangoproject.com/en/1.11/ref/models/fields/ https://docs.djangoproject.com/en/1.11/topics/db/models/#automatic-primary-key-fields
【讨论】:
以上是关于Django - Python 3 - “AssertionError:一个模型不能有多个 AutoField。”的主要内容,如果未能解决你的问题,请参考以下文章