psycopg2.ProgrammingError:关系“事件"不存在 [英] psycopg2.ProgrammingError: relation "event" does not exist

查看:594
本文介绍了psycopg2.ProgrammingError:关系“事件"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Alembic和flask-migrate以及Postgres一起用于我的数据库.我已经运行了数据库初始化和数据库迁移.但是,当我运行db upgrade命令时,出现以下错误:

I am using alembic along with flask-migrate along with Postgres for my database. I have ran a db init and db migrate. However when i run the db upgrade command, I get the following error:

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

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

我非常清楚为什么会发生错误.该脚本正在尝试创建一个Attendee表,该表引用了稍后在脚本中创建的Event表.

I am very well aware of why the error is happening. The script is trying to create the Attendee table which references an Event table that is created later in the script.

我的问题是我之间有很多关系,并且我认为重新排列脚本中的每个表以使其建立起来都没有道理. Alembic和flask-migrate不应该能够执行标准的创建表脚本,该脚本列出多个关系而不会失败,只要这些关系都在脚本中定义即可.这是我的Alembic脚本,带有一些修改.

My question is I have a lot of relationships and I don't think it makes sense to rearrange every table in the script in order to get it to build. Shouldn't alembic and flask-migrate be able to do a standard create table script that lists multiple relationships without failing as long as those relationships are all defined in the script. This is my alembic script with some redaction.

    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')
    )

要回答一个明显的问题,如果我在表创建脚本中移动,它会在另一个表处失败,因为与会者引用了另外3个表,而这些表也引用了其他表,如果这些表首先被创建,则会失败.

To answer the obvious question, if i move around the table creation script, it fails at a different table because attendee references 3 other tables, and those tables refer other tables as well which would fail if they were created first.

推荐答案

我发现了问题所在. db.create_all()完成后,您只能使用db init,迁移,更新脚本.

I figured out the issue. You can only use the db init, migrate, update scripts after the db.create_all() is done.

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

Details can be found here:

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/

这篇关于psycopg2.ProgrammingError:关系“事件"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆