2017-04-18 6 views
1

私はこれに夢中になっています。Django - Python 3 - "AssertionError:モデルは複数のAutoFieldを持つことはできません。"

私はモデルのコードを取得するには、端末のコマンドを使用されるよりも、私はMySQLWorkbench

This his my Schema

と私のデータベースを作成しました:

$python3 manage.py inspectie 

私のmodels.pyにコードを通過した後、私はしてみてくださいシェルでモデルを使用するには

$ 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から

答えて

3

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) 
+0

私の問題は私が自動的に追加された列ものAutoFieldであることに気づいていなかったということでした。見積もりやヒントなどをありがとう! – Anthony

1

はあなたでは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

+0

あなたは正しいです、ありがとう! – Anthony

関連する問題