我正在尝试使用Alembic为两个模型创建带有--autogenerate
的修订,但是收到重复的表键错误。是否需要指定架构?如果是这样,如何设置?我读过的文档说要使用__table_args__ = {'schema': 'somename'}
,但这并没有帮助。任何提示或建议都将不胜感激。
我当前的设置是:
base.py
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
workspace.py
from sqlalchemy import Column, Integer, String
from base import Base
class WorkspaceModel(Base):
__tablename__ = 'workspaces'
id = Column(Integer, primary_key=True)
name = Column(String)
host.py
from sqlalchemy import Column, Integer, String
from base import Base
class HostModel(Base):
__tablename__ = 'hosts'
id = Column(Integer, primary_key=true)
ip = Column(String)
alembic / env.py
from host import HostModel
from workspace import WorkspaceModel
target_metadata = [HostModel.metadata, WorkspaceModel.metadata]
错误
ValueError: Duplicate table keys across multiple MetaData objects: "hosts", "workspaces"
引用univerio的答案,这节省了我的时间。
target_metadata应该只是target_metadata = Base.metadata使用Base.metadata并不意味着您可以从主机导入HostModel和工作区导入WorkspaceModel中删除导入]
对我有用。