mysql - Django - Python 3 - "AssertionError: A model can' t 有多个 AutoField。”

标签 mysql django python-3.x django-models mysql-workbench

我快被这个搞疯了。

我用 MySQLWorkbench 创建了我的数据库

This his my Schema

比起我使用终端命令获取模型代码:

$python3 manage.py inspectdb

将代码传递给我的 models.py 后,我尝试在 shell 中使用模型

$python3 manage.py 外壳

但是我总是得到这个错误:

"AssertionError: A model can't have more than one AutoField."

但是错误没有意义,因为每个模型中只有一个 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'

我只是不明白这个问题!!!

最佳答案

来自 docs :

By default, Django gives each model the following field:

id = models.AutoField(primary_key=True)

This is an auto-incrementing primary key.

If you’d like to specify a custom primary key, just specify primary_key=True on one of your fields. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.

Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

所以尝试像这样设置 primary_key=True:

bid = models.AutoField(db_column='BID', primary_key=True)

关于mysql - Django - Python 3 - "AssertionError: A model can' t 有多个 AutoField。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43472012/

相关文章:

python - Django 开发服务器似乎使用旧版本的 python 源文件

python - 如何在 Django 错误电子邮件中包含服务器主机名?

python - 弃用警告 : invalid escape sequence - what to use instead of\d?

python - 从循环 if 语句中提取 pandas timeseries 数据帧的子集

javascript - 通过javascript将数据发布到数据库会导致编码问题

mysql - SQL同表相关子查询

PHP 更新 MySQL 数据库表

python random.random() 导致 "' 模块的对象不可调用”当用于自定义模板标签时

python - 尝试将一个字符串拆分成一个列表,将列表的顺序反转,然后打印出反转后的字符串的字符串

mysql - 无法从已部署的 Kubernetes NodeJS 应用程序连接到 Google MySQL