postgresql Django:ProgrammingError:列“id”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39812384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django : ProgrammingError: column "id" does not exist
提问by Preumsse
I try to use postgresql database (before I had SQLite) but I have a message when I execute python manage.py migrate
:
我尝试使用 postgresql 数据库(在我使用 SQLite 之前),但是当我执行时有一条消息python manage.py migrate
:
Operations to perform:
Apply all migrations: sessions, admin, sites, auth, contenttypes, main_app
Running migrations:
Rendering model states... DONE
Applying main_app.0004_auto_20161002_0034...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 482, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql/schema.py", line 110, in _alter_field
new_db_params, strict,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 634, in _alter_field
params,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "id" does not exist
LINE 1: ..._app_projet" ALTER COLUMN "id" TYPE integer USING "id"::inte...
So I guess this error is about my model 'Projet' but this model has ID field ! I see that in 0001_initial.py:
所以我猜这个错误是关于我的模型“Projet”,但这个模型有 ID 字段!我在0001_initial.py 中看到:
...
migrations.CreateModel(
name='Projet',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(db_index=True, max_length=30)),
('presentation', models.TextField(max_length=2500, null=True)),
...
My migration main_app.0004_auto_20161002_0034 :
我的迁移 main_app.0004_auto_20161002_0034 :
class Migration(migrations.Migration):
dependencies = [
('main_app', '0003_auto_20161002_0033'),
]
operations = [
migrations.AlterField(
model_name='projet',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
Projet model is like that :
项目模型是这样的:
class Group(models.Model) :
name = models.CharField(max_length=30, db_index=True)
class Meta :
abstract = True
unique_together = (("id", "name"),)
class Projet(Group) :
presentation = models.TextField(max_length=2500, null=True)
Realy I don't understand why I have this error... my settings configuration database is good, all others models work, ...
真的我不明白为什么我会出现这个错误......我的设置配置数据库很好,所有其他模型都可以工作,......
If I forgot something, please don't hesitate to tell me ! I need realy your helps !
如果我忘记了什么,请不要犹豫告诉我!我真的需要你的帮助!
回答by John Gordon
Your model definition doesn't identify any of the fields as a primary key, therefore Django assumes a primary key column named id
. However this column doesn't actually exist in the table.
您的模型定义未将任何字段标识为主键,因此 Django 假定主键列名为id
. 但是,该列实际上并不存在于表中。
Change your model definition to designate one of the fields as a primary key, and Django won't try to look for an id
column.
更改模型定义以将其中一个字段指定为主键,Django 不会尝试查找id
列。
回答by Michael Samoylov
class Group(models.Model) :
name = models.CharField(max_length=30, unique=True)
class Meta:
abstract = True
class Projet(Group) :
presentation = models.CharField(max_length=2500, blank=True)
Remove your migration and create a new one.
删除您的迁移并创建一个新迁移。
Just adding my two cents.
只需加上我的两分钱。
Nullable fields:
可空字段:
Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it's redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.
See https://docs.djangoproject.com/en/1.10/ref/models/fields/#null
避免在基于字符串的字段(例如 CharField 和 TextField)上使用 null,因为空字符串值将始终存储为空字符串,而不是 NULL。如果基于字符串的字段具有 null=True,则意味着它有两个可能的“无数据”值:NULL 和空字符串。在大多数情况下,“无数据”有两个可能的值是多余的;Django 约定是使用空字符串,而不是 NULL。
见https://docs.djangoproject.com/en/1.10/ref/models/fields/#null
max_length:
最长长度:
If you specify a max_length attribute, it will be reflected in the Textarea widget of the auto-generated form field. However it is not enforced at the model or database level. Use a CharField for that.
https://docs.djangoproject.com/en/1.10/ref/models/fields/#textfield
如果您指定 max_length 属性,它将反映在自动生成的表单字段的 Textarea 小部件中。但是,它不会在模型或数据库级别强制执行。为此使用 CharField。
https://docs.djangoproject.com/en/1.10/ref/models/fields/#textfield
回答by Rene Montesardo
if the id column is missing, just add the column in Postgres itself. Go to pgadmin than to the table and there create the column id.
如果缺少 id 列,只需在 Postgres 本身中添加该列。转到 pgadmin 而不是表,然后在那里创建列 id。
Than back to Django makemigrations than migrate. If this will fail, delete everything in the migration folder of the app without init.py and delete everything in the folder pycache
比回 Django makemigrations 比 migrate。如果这会失败,请删除应用程序迁移文件夹中的所有内容,而无需init.py 并删除文件夹pycache 中的所有内容
than makemigrations followed by migrate and it should work.
而不是 makemigrations 然后是 migrate ,它应该可以工作。