alembic 自动生成修订版尝试删除我数据库的所有索引和表

问题描述 投票:0回答:2

我正在开发一个使用 GCP SQL 数据库的现有 FastAPI 应用程序。我在本地对数据库模式做了一些小的更改,并创建了一个本地 postgresql 数据库来测试我的更改。我使用 alembic 自动生成迁移,但它生成了一个奇怪的更改,旨在删除数据库的索引和表。我确信我错过了一个重要的步骤,任何有alembic经验的人都可以帮助我摆脱困境。

"""user_namesapace_revision

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_delegate_approver_email', table_name='delegate')
    op.drop_index('ix_delegate_delegate_email', table_name='delegate')
    op.drop_index('ix_delegate_end_date', table_name='delegate')
    op.drop_index('ix_delegate_id', table_name='delegate')
    op.drop_index('ix_delegate_start_date', table_name='delegate')
    op.drop_table('delegate')
    op.drop_table('workflow_namespaces')
    op.drop_index('ix_request_workflow_approvers_id', table_name='request_workflow')
    op.drop_index('ix_request_workflow_created_at', table_name='request_workflow')
    op.drop_index('ix_request_workflow_end_process', table_name='request_workflow')
    op.drop_index('ix_request_workflow_id', table_name='request_workflow')
    op.drop_index('ix_request_workflow_position', table_name='request_workflow')
    op.drop_index('ix_request_workflow_sous_position', table_name='request_workflow')
    op.drop_index('ix_request_workflow_status', table_name='request_workflow')
    op.drop_index('ix_request_workflow_updated_at', table_name='request_workflow')
    op.drop_index('ix_request_workflow_workflow_step_id', table_name='request_workflow')
    op.drop_table('request_workflow')
    op.drop_index('ix_users_active_namespace', table_name='users')
    op.drop_index('ix_users_created_at', table_name='users')
    op.drop_index('ix_users_email', table_name='users')
    op.drop_index('ix_users_first_name', table_name='users')
    op.drop_index('ix_users_id', table_name='users')
    op.drop_index('ix_users_language', table_name='users')
    op.drop_index('ix_users_last_name', table_name='users')
    op.drop_index('ix_users_line_manager', table_name='users')
    op.drop_index('ix_users_namespace', table_name='users')
    op.drop_index('ix_users_password', table_name='users')
    op.drop_index('ix_users_role', table_name='users')
    op.drop_index('ix_users_token', table_name='users')
    op.drop_index('ix_users_updated_at', table_name='users')
    op.drop_table('users')
    op.drop_index('ix_workflow_condition_approver_id', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_criteria', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_end_process', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_id', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_operator', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_reviewerField', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_type', table_name='workflow_condition')
    op.drop_index('ix_workflow_condition_value', table_name='workflow_condition')
    op.drop_table('workflow_condition')
    op.drop_index('ix_request_created_at', table_name='request')
    op.drop_index('ix_request_current_step_position', table_name='request')
    op.drop_index('ix_request_dataObj', table_name='request')
    op.drop_index('ix_request_editUrl', table_name='request')
    op.drop_index('ix_request_element_id', table_name='request')
    op.drop_index('ix_request_loop', table_name='request')
    op.drop_index('ix_request_request_id', table_name='request')
    op.drop_index('ix_request_status', table_name='request')
    op.drop_index('ix_request_timeZone', table_name='request')
    op.drop_index('ix_request_updated_at', table_name='request')
    op.drop_table('request')
    op.drop_index('ix_request_actions_action', table_name='request_actions')
    op.drop_index('ix_request_actions_approvers_id', table_name='request_actions')
    op.drop_index('ix_request_actions_comment', table_name='request_actions')
    op.drop_index('ix_request_actions_created_at', table_name='request_actions')
    op.drop_index('ix_request_actions_id', table_name='request_actions')
    op.drop_index('ix_request_actions_loop', table_name='request_actions')
    op.drop_index('ix_request_actions_request_workflow_id', table_name='request_actions')
    op.drop_index('ix_request_actions_workflow_id', table_name='request_actions')
    op.drop_table('request_actions')
    op.drop_table('users_namespaces')
    op.drop_index('ix_workflow_created_at', table_name='workflow')
    op.drop_index('ix_workflow_id_template', table_name='workflow')
    op.drop_index('ix_workflow_id_workflow', table_name='workflow')
    op.drop_index('ix_workflow_name', table_name='workflow')
    op.drop_index('ix_workflow_status', table_name='workflow')
    op.drop_index('ix_workflow_storeResponses', table_name='workflow')
    op.drop_index('ix_workflow_type', table_name='workflow')
    op.drop_index('ix_workflow_url', table_name='workflow')
    op.drop_table('workflow')
    op.drop_index('ix_namespace_billingEmail', table_name='namespace')
    op.drop_index('ix_namespace_created_at', table_name='namespace')
    op.drop_index('ix_namespace_id', table_name='namespace')
    op.drop_index('ix_namespace_name', table_name='namespace')
    op.drop_index('ix_namespace_owner_id', table_name='namespace')
    op.drop_index('ix_namespace_plan', table_name='namespace')
    op.drop_index('ix_namespace_quota', table_name='namespace')
    op.drop_index('ix_namespace_status', table_name='namespace')
    op.drop_index('ix_namespace_updated_at', table_name='namespace')
    op.drop_index('ix_namespace_usedQuota', table_name='namespace')
    op.drop_table('namespace')
    op.drop_index('ix_addon_config_ccEmails', table_name='addon_config')
    op.drop_index('ix_addon_config_ccType', table_name='addon_config')
    op.drop_index('ix_addon_config_column', table_name='addon_config')
    op.drop_index('ix_addon_config_config_id', table_name='addon_config')
    op.drop_index('ix_addon_config_emailLanguage', table_name='addon_config')
    op.drop_index('ix_addon_config_finalApproval', table_name='addon_config')
    op.drop_index('ix_addon_config_formAccess', table_name='addon_config')
    op.drop_index('ix_addon_config_onHold', table_name='addon_config')
    op.drop_index('ix_addon_config_sendBack', table_name='addon_config')
    op.drop_index('ix_addon_config_sheetId', table_name='addon_config')
    op.drop_index('ix_addon_config_sheetName', table_name='addon_config')
    op.drop_index('ix_addon_config_template', table_name='addon_config')
    op.drop_index('ix_addon_config_triggerOwner', table_name='addon_config')
    op.drop_index('ix_addon_config_triggerStatus', table_name='addon_config')
    op.drop_index('ix_addon_config_validationReminder', table_name='addon_config')
    op.drop_index('ix_addon_config_validationReminderTime', table_name='addon_config')
    op.drop_table('addon_config')
    op.drop_index('ix_workflow_steps_id', table_name='workflow_steps')
    op.drop_index('ix_workflow_steps_name', table_name='workflow_steps')
    op.drop_index('ix_workflow_steps_parent_id', table_name='workflow_steps')
    op.drop_index('ix_workflow_steps_position', table_name='workflow_steps')
    op.drop_index('ix_workflow_steps_sous_position', table_name='workflow_steps')
    op.drop_table('workflow_steps')
   
database migration fastapi alembic
2个回答
1
投票

您可能在 alembic 的

env.py
文件中犯了错误。如果您使用
autogenerate
,您应该导入所有模型。

看看这个答案


0
投票
# models.py
from database import Base

class User(Base):
    __tablename__ = "users"
    ....

class User(Base):
    __tablename__ = "users"
    ....

# database.py
....
Base = declarative_base(metadata=MetaData(schema="data"))


# alembic/env.py
from database import Base
from models import Booking, User

target_metadata = Base.metadata
© www.soinside.com 2019 - 2024. All rights reserved.