我有一个遗留的数据库,我上传到PostgreSQL,并与Django连接。然后,我直接在PostgreSQL中运行SQL脚本来更改数据库(我删除了一些列),然后手动删除了djangomodels.py中的一些列。从那时起,我无法进行任何迁移,每当我尝试迁移时,Django都试图删除我手动删除的列,并给出了下面的错误.我如何同步我的DB和Django项目,以便我可以对项目进行修改?
django.db.utils.ProgrammingError: column "confidence" of relation "Location" does not exist
编辑:
$ python manage.py migrate
D:\projects\map\dataXProject
(0.328)
SELECT
c.relname,
CASE
WHEN c.relispartition THEN 'p'
WHEN c.relkind IN ('m', 'v') THEN 'v'
ELSE 't'
END
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid)
; args=None; alias=default
(0.250) SELECT "django_migrations"."id", "django_migrations"."app", "django_migrations"."name", "django_migrations"."applied" FROM "django_migrations"; args=(); alias=default
(0.235)
SELECT
c.relname,
CASE
WHEN c.relispartition THEN 'p'
WHEN c.relkind IN ('m', 'v') THEN 'v'
ELSE 't'
END
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid)
; args=None; alias=default
(0.343) SELECT "django_migrations"."id", "django_migrations"."app", "django_migrations"."name", "django_migrations"."applied" FROM "django_migrations"; args=(); alias=default
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, sites
Running migrations:
(0.281)
SELECT
c.relname,
CASE
WHEN c.relispartition THEN 'p'
WHEN c.relkind IN ('m', 'v') THEN 'v'
ELSE 't'
END
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid)
; args=None; alias=default
Applying sites.0008_remove_location_approved_remove_location_confidence_and_more...ALTER
TABLE "Location" DROP COLUMN "approved" CASCADE; (params ())
(0.718) ALTER TABLE "Location" DROP COLUMN "approved" CASCADE; args=(); alias=default
ALTER TABLE "Location" DROP COLUMN "confidence" CASCADE; (params ())
(0.328) ALTER TABLE "Location" DROP COLUMN "confidence" CASCADE; args=(); alias=default
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "confidence" of relation "Location" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\projects\map\dataXProject\manage.py", line 22, in <module>
main()
File "D:\projects\map\dataXProject\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
self.execute(*args,**cmd_options)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 460, in execute
output = self.handle(*args,**options)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 98, in wrapped
res = handle_func(*args,**kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\migration.py", line 131, in apply
operation.database_forwards(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\migrations\operations\fields.py", line 170, in database_forwards
schema_editor.remove_field(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 683, in remove_field
self.execute(sql)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\base\schema.py", line 192, in execute
cursor.execute(sql, params)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 103, in execute
return super().execute(sql, params)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "confidence" of relation "Location" does not exist
1条答案
按热度按时间eagi6jfj1#
即使是在运行自定义SQL时,也应该尝试使用迁移。这样可以防止出现此类情况。
但是,如果更改已应用于数据库,则可以使用
./manage.py migrate
的--fake
标志跳过单个迁移https://docs.djangoproject.com/en/4.1/ref/django-admin/#cmdoption-migrate-fake
如果没有此选项,可以使用直接SQL命令将名为
confidence
的列添加到Location
表中。然后,迁移将删除此列,以使您与model数据库图形同步。