- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我快被这个搞疯了。
我用 MySQLWorkbench 创建了我的数据库
比起我使用终端命令获取模型代码:
$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 specifyprimary_key=True on one of your fields. If Django sees you’veexplicitly set Field.primary_key, it won’t add the automatic idcolumn.
Each model requires exactly one field to have primary_key=True (eitherexplicitly 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/
我们正在处理的 Django 项目和我们的 postgresql 数据库有一个小问题。 我们正在进行的项目是从 PHP 站点到 Django 站点的站点/数据库转换。所以我们使用 inspect db
我有一个这样的模型 class Alrule(models.Model): priority = models.PositiveIntegerField(unique=True) ru
如何获取 AutoField 的计数器值,例如大多数模型常用的 id 字段? 此刻,我做的是: MyModel.objects.latest('id').id 但是当所有对象都已从数据库中删除时,这就
是否有可能在 Django 中创建模型(或多对多关系)而无需数据库中的 id 字段 (AutoField)? 例如,我有模型:Task 和 User。用户可以分配许多任务,任务可以分配给许多用户。通常
我希望我的模型的主键是未签名的。因此我做这样的事情: class MyModel(models.Model): id = models.PositiveIntegerField(primary
我有一个具有唯一整数的模型,需要根据外键递增,以下代码是我当前处理它的方式: class MyModel(models.Model): business = models.ForeignKey
我在 Django 1.8 中遇到一个奇怪的错误: 'AutoField' object has no attribute 'remote_field' 我有一个像这样的模型: from django
在 Django 1.8.11 中,使用 PostgreSQL 后端,如果我有一个包含 3 个项目的表(id 为 1、2、3),我像这样手动插入一个项目,明确指定 id: i = MyItem(id
这个问题随机开始出现在我身上。我知道 Django 会生成自己的 ID,但我的很多代码一直在使用一直有效的自定义 AutoFields。我今天添加了一个新类并尝试进行迁移,但此错误一直出现。 我确实删
Django 1.7.1、MySQL 5.6、Python 2.7.8 我有一个看起来像这样的模型: class Host(models.Model): hostName = models.C
所以我从 django 3.1 升级到 3.2 并且在我进行迁移时在我的两个模型上它一直迫使我将自动 ID 更改为 BigAutoField 即使我有(并且有) DEFAULT_AUTO_FIELD
在使用 postgresql 的 Django 项目中,我曾经在数据库中插入了一个条目,有一天。有权访问它的其他人已使用 pgadmin4 在数据库中手动添加了 36 个其他行。当我想使用 Djang
我有现有的数据库,我已使用 SQLAlchemy 将其迁移到新的 PostgreSQL 数据库。 我移动了所有具有与以前相同值的主键。现在我的表中填满了数据,但关联的序列从 1 开始。我将 pk 值存
我正在构建一个 Multi-Tenancy 应用程序,其数据模型可简化为: class Tenant(models.Model): name = models.CharField(max_le
我们正在迁移我们的 Oracle 数据库并对其进行必要的更改,一个主要更改是我们将 UUIDField 添加为所有模型的主键(对客户端隐藏),并且(尝试添加) 一个常规的 AutoField。 我们发
对于我们的 Django 应用程序,我们希望获得一个 AutoField 以从 1 以外的数字开始。似乎没有明显的方法可以做到这一点。有什么想法吗? 最佳答案 就像其他人所说的那样,这在数据库端比在
我需要通过单击按钮来复制一组输入字段;根据需要多次。 此功能与我们在 Liferay 中的功能相同: 转到“控制面板 -> 用户”,单击任意用户。 在页面右侧的“标识”下;点击“地址、电话号码”。 单
我的类应用程序的 models.py: Teacher 和 StudentInfo 将存储各自的详细信息。 from form.models import StudentInfo from teach
我有一个旧数据库,其中一个整数设置为主键。它最初是手动管理的,但由于我们想迁移到 django,管理工具似乎是正确的起点。我创建了模型并尝试将主键设置为自动字段。它似乎不记得更新中的旧ID,并且它不会
我目前有一个 Django 支持的生产网络应用程序,它包含多个模型,位于 Postgresql 数据库 (Google Cloud SQL) 之上 在初始设置期间,其中一个模型设置如下: class
我是一名优秀的程序员,十分优秀!