心理战2.编程错误:关系"event"不存在



我的数据库使用alembic和flask migrate以及Postgres。我已经运行了数据库初始化和数据库迁移。然而,当我运行数据库升级命令时,我会得到以下错误:

cursor.execute(statement, parameters)
psycopg2.ProgrammingError: relation "event" does not exist
The above exception was the direct cause of the following exception:

我很清楚为什么会发生错误。脚本正在尝试创建"与会者"表,该表引用了稍后在脚本中创建的"事件"表。

我的问题是,我有很多关系,我认为为了构建它而重新排列脚本中的每个表是没有意义的。难道alembic和flask migrate不应该做一个标准的创建表脚本,只要这些关系都在脚本中定义,就可以列出多个关系而不失败吗。这是我的alembic脚本,经过了一些编辑。

op.create_table('attendee',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('event_id', sa.Integer(), nullable=False),
sa.Column('event_plan_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['event_id'], ['event.id'], ),
sa.ForeignKeyConstraint(['event_plan_id'], ['event_plan.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('event',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('event_name', sa.String(length=128), nullable=False),
sa.Column('description', sa.String(length=128), nullable=False),
sa.Column('organizer_id', sa.Integer(), nullable=False),
sa.Column('location_id', sa.Integer(), nullable=False),
sa.Column('attendee_id', sa.Integer(), nullable=False),
sa.Column('group_chat_id', sa.Integer(), nullable=False),
sa.Column('event_type_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['attendee_id'], ['attendee.id'], ),
sa.ForeignKeyConstraint(['event_type_id'], ['event_type.id'], ),
sa.ForeignKeyConstraint(['group_chat_id'], ['group_chat.id'], ),
sa.ForeignKeyConstraint(['location_id'], ['location.id'], ),
sa.ForeignKeyConstraint(['organizer_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)

为了回答这个显而易见的问题,如果我在表创建脚本中移动,它会在另一个表中失败,因为attender引用了另外3个表,而这些表也引用了其他表,如果它们是先创建的,则会失败。

我发现了这个问题。您只能在db.create_all((完成后使用db-init、migrate和update脚本。

详细信息可以在这里找到:

http://flask-sqlalchemy.pocoo.org/2.3/quickstart/

如果你得到以下错误:

No application found. Either work inside a view function or push an  application context.

检查以下文章:http://flask-sqlalchemy.pocoo.org/2.3/contexts/

最新更新