psycopg2.ProgrammingError: relation "event" does not exist

psycopg2.ProgrammingError: relation "event" does not exist

我正在为我的数据库使用 alembic 和 flask-migrate 以及 Postgres。我有 运行 个 db init 和 db migrate。但是,当我 运行 db upgrade 命令时,出现以下错误:

cursor.execute(statement, parameters)
psycopg2.ProgrammingError: relation "event" does not exist

The above exception was the direct cause of the following exception:

我很清楚错误发生的原因。该脚本正在尝试创建 Attendee table,它引用稍后在脚本中创建的事件 table。

我的问题是我有很多关系,我认为在脚本中每 table 后退 运行ge 以构建它是没有意义的。 alembic 和 flask-migrate 不应该能够执行一个标准的 create table 脚本来列出多个关系而不会失败,只要这些关系都在脚本中定义。这是我的经过一些编辑的 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')
    )

为了回答这个显而易见的问题,如果我移动 table 创建脚本,它会在不同的 table 处失败,因为与会者引用了另外 3 个 table,而那些 tables 也引用其他 tables,如果先创建它们将会失败。

我想通了。您只能在 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/