psycopg2.ProgrammingError: relation “event” does not exist

对着背影说爱祢 提交于 2019-12-12 01:17:36

问题


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:

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.

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

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.


回答1:


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/

If you get the following error:

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

Check the following article: http://flask-sqlalchemy.pocoo.org/2.3/contexts/



来源:https://stackoverflow.com/questions/53565293/psycopg2-programmingerror-relation-event-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!