我正在尝试对由 Django ORM 管理的表进行 FK,但是 SQLA 好像不太喜欢。
class SomeSAModel(DeclarativeBase):
user_id: Mapped[int] = mapped_column(
sa.ForeignKey("users_customuser.id") # this is a reference to a Django table
)
这是我运行时遇到的错误
alembic revision --autogenerate
File "/home/dev/Desktop/t5hob/Backend/alembic/env.py", line 137, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/runtime/environment.py", line 946, in run_migrations
self.get_context().run_migrations(**kw)
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/runtime/migration.py", line 616, in run_migrations
for step in self._migrations_fn(heads, self):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/command.py", line 212, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/api.py", line 570, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/api.py", line 617, in _run_environment
compare._populate_migration_script(
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/compare.py", line 65, in _populate_migration_script
_produce_net_changes(autogen_context, upgrade_ops)
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/compare.py", line 98, in _produce_net_changes
comparators.dispatch("schema", autogen_context.dialect.name)(
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/util/langhelpers.py", line 310, in go
fn(*arg, **kw)
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/compare.py", line 134, in _autogen_for_tables
[(table.schema, table.name) for table in autogen_context.sorted_tables]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 1141, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/alembic/autogenerate/api.py", line 482, in sorted_tables
result.extend(m.sorted_tables)
^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/schema.py", line 5626, in sorted_tables
return ddl.sort_tables(
^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/ddl.py", line 1252, in sort_tables
for (t, fkcs) in sort_tables_and_constraints(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/ddl.py", line 1328, in sort_tables_and_constraints
dependent_on = fkc.referred_table
^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/schema.py", line 4753, in referred_table
return self.elements[0].column.table
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 1141, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/schema.py", line 3170, in column
return self._resolve_column()
^^^^^^^^^^^^^^^^^^^^^^
File "/home/dev/Desktop/t5hob/Backend/.venv/lib/python3.12/site-packages/sqlalchemy/sql/schema.py", line 3193, in _resolve_column
raise exc.NoReferencedTableError(
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'incidents__incident.ack_by' could not find table 'users_customuser' with which to generate a foreign key to target column 'id'
如果Django管理该表,则列名中将添加FK的后缀(
_id
)。您应该显式地将列名称传递给mapped_column。
据我记得,是这样的:
mapped_column("customuser_id", sa.ForeignKey("users_customuser.id"))
仔细检查列名称。
不过,我还是不明白为什么你还使用 Alembic 作为 Django 管理的数据库。