2016-10-01 47 views
2

我嘗試使用PostgreSQL數據庫(之前,我有SQLite的),但我有一個消息當我執行python manage.py migrateDjango的:ProgrammingError:列「ID」不存在

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... 

所以我想這個誤差約爲我的模型'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)), 
... 

我的遷移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'), 
    ), 
    ] 

謨模式是這樣的:

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) 

真的,我不明白爲什麼我有這個錯誤...我的設置配置數據庫是好的,所有其他模型的工作,...

如果我忘記了什麼,請不要猶豫告訴我!我需要你的幫助!

+0

爲什麼你'unique_together =((ID」, 「姓名」))'主鍵ID是由已獨特本身,所以把它包含在'unique_together'中是沒有意義的,回溯顯示'0004_auto_20161002_0034'導致錯誤,所以你應該在你的問題中包含這個遷移。 – Alasdair

+0

是的確是這樣,我刪除了unique_together。包括0004_auto .. – Preumsse

+0

你有沒有機會讓'Group''抽象「在遷移時,當它不是」抽象「的時候?非抽象模型的子模型不會有'id'列... – AKX

回答

2

您的模型定義沒有將任何字段標識爲主鍵,因此Django假定名爲id的主鍵列。但是這個列在表中並不存在。

更改您的模型定義,將其中一個字段指定爲主鍵,Django不會嘗試查找id列。

+0

但是在Django中,如果我不創建一個字段作爲主鍵,Django就會這樣做!證明:在0001_initial.py中,主鍵是自動創建的... – Preumsse

+0

是的。如果Django不存在,Django隱式創建一個自動增量整數主鍵列,所以這不是問題。 – AKX

+0

@AKX Django假設存在一個「id」列,但我不相信它會創建它。 –

2
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) 

刪除您的遷移並創建一個新遷移。

只需加我兩分錢。

可空字段:

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

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

+0

我從我的模型中刪除'default = True'並將其替換爲默認值=「」,但我遇到同樣的問題 – Preumsse

相關問題