问题描述
我正在使用Django应用程序的Postgres,我想进行模型更改,重置一行,因此它不再是主键。
这是我在Django目前看起来像我的模型:
class Meeting(TimeStampedModel):
row = models.CharField(max_length = 20,primary_key = True)
total_items = models.IntegerField()
我已经运行 django-admin.py flush
以从数据库中删除所有数据。如果我运行 python manage.py makemigrations
我看到没有检测到变化
。所以我们从一个干净的基地开始。
现在我在models.py中编辑行
,所以它不再主键:
row = models.CharField(max_length = 20)
/ pre>
并运行
python manage.py makemigrations
并设置1
作为默认值:您正在尝试添加一个不可为空的字段'id'默认;我们不能这样做(数据库需要填充现有行的东西)。
请选择一个修复:
1)现在提供一次性默认(将在所有现有行上设置)
2)退出,并让我在models.py $中添加一个默认值b $ b选择一个选项:1
请现在输入默认值作为有效的Python
可以使用datetime和django.utils.timezone模块,因此您可以timezone.now()
>>> 1
'frontend'的迁移:
0007_auto_20150305_1129.py:
- 添加字段ID以满足
- 在会议上更改字段行
似乎运行正常。然后,如果我运行
python manage.py migrate
:$ python manage .py migrate
要执行的操作:
同步未迁移的应用程序:debug_toolbar
应用所有迁移:contenttypes,前端,站点,auth,会话
不迁移同步应用程序:
创建表...
安装自定义SQL ...
安装索引...
运行迁移:
应用frontend.0007_auto_20150305_1129 ...追溯(最近的呼叫最后):
文件manage.py,第10行,在< module>
execute_from_command_line(sys.argv)
文件/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/core/management/__init__.py,第385行,在execute_from_command_line
utility.execute()
...
文件/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/db/backends/ utils.py,第65行,执行
return self.cursor.execute(sql,params)
django.db.utils.ProgrammingError:为表frontend_meeting的id列指定了多个默认值
为什么告诉我我有
多个默认值
?
我需要设置一个唯一的主键值作为默认值 - 如果是,我该怎么做?
这是迁移文件的样子:
class Migration(migrations.Migration):
依赖关系= [
('frontend','0006_auto_20150305_1050'),
]
operations = [
migrations.AddField(
model_name ='meeting',
name ='id',
field = models.AutoField(auto_created = True,primary_key = True,default = 1,serialize = False, verbose_name ='ID'),
preserve_default = False,
),
migrations.AlterField(
model_name ='meeting',
name ='row',
field = models.CharField(max_length = 20),
preserve_default = True,
),
]
解决方案尝试使用中间迁移来实现:
1)添加
id = models.IntegerField()
到你的模型。
运行 makemigrations ,然后迁移。
2)从行中删除primary_key = True -field,并删除'id'字段。再次运行 makemigrations 和迁移。
I am working with Postgres on a Django app and I want to make a model change, resetting a row so it is no longer the primary key.
This is what my model currently looks like in Django:
class Meeting(TimeStampedModel): row = models.CharField(max_length=20, primary_key=True) total_items = models.IntegerField()
I have run
django-admin.py flush
to remove all data from the database. If I runpython manage.py makemigrations
I seeNo changes detected
. So we are starting from a clean base.Now I edit
row
in models.py so it is no longer the primary key:row = models.CharField(max_length=20)
And run
python manage.py makemigrations
and set1
as the default when asked:You are trying to add a non-nullable field 'id' to meeting without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now() >>> 1 Migrations for 'frontend': 0007_auto_20150305_1129.py: - Add field id to meeting - Alter field row on meeting
That seems to run OK. Then if I run
python manage.py migrate
:$ python manage.py migrate Operations to perform: Synchronize unmigrated apps: debug_toolbar Apply all migrations: contenttypes, frontend, sites, auth, sessions Synchronizing apps without migrations: Creating tables... Installing custom SQL... Installing indexes... Running migrations: Applying frontend.0007_auto_20150305_1129...Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() ... File "/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "frontend_meeting"
Why is it telling me I have
multiple default values
?Do I need to set a unique primary key value as a default - and if so, how can I do this?
This is what the migrations file looks like:
class Migration(migrations.Migration): dependencies = [ ('frontend', '0006_auto_20150305_1050'), ] operations = [ migrations.AddField( model_name='meeting', name='id', field=models.AutoField(auto_created=True, primary_key=True, default=1, serialize=False, verbose_name='ID'), preserve_default=False, ), migrations.AlterField( model_name='meeting', name='row', field=models.CharField(max_length=20), preserve_default=True, ), ]
解决方案Try to use an intermediate migration to achieve this:
1) Add
id = models.IntegerField()
to your model.Run makemigrations and then migrate.2) Remove primary_key=True from the 'row'-field, and also remove the 'id'-field. Again run makemigrations and migrate.
这篇关于Django迁移:使行不是主键,获取“ProgrammingError:多个默认值”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!