I'am building a flask-sqlalchemy data model and had success creating the tables and adding data to the tables in the database. I recently decided to include Flask-Migrate around the app so I can track changes conveniently.
Using Flask-Migrate along with Flask-Script Manager I was successfully able to initialise the database using
python model.py db init
and able to create a migration using
python model.py db migrate
but it appears that Flask-Migrate doesn't know how to handle manually declared sequences because when I run
python model.py db upgrade
i am hit with a
psycopg2.ProgrammingError: relation "seq_inbox" does not exist
seq_inbox being the name of my manually declared sequence.
Upon inspection of the created migration in the migrations/versions directory I see this
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('inbox',
sa.Column('inbox_id', sa.Integer(), server_default=sa.text("nextval('seq_inbox')"), nullable=False),
sa.Column('message_title', sa.String(length=60), server_default='n/a', nullable=False),
sa.Column('first_name', sa.String(length=30), server_default='n/a', nullable=False),
sa.Column('last_name', sa.String(length=30), server_default='n/a', nullable=False),
sa.Column('email', sa.String(length=60), nullable=False),
sa.Column('phone_number', sa.String(length=60), server_default='n/a', nullable=False),
sa.Column('message', sa.String(length=1000), server_default='n/a', nullable=False),
sa.PrimaryKeyConstraint('inbox_id')
)
It looks to me like Alembic is interpreting my sequence name as plain text, is this correct?
How can I fix this "relation does not exist" error?
EDIT 1 When I remove the sequence declaration and reset everything I am able to run the upgrade command successfully without a hitch. But I want my named sequences to be present.
EDIT 2
Here is the error after running python models.py db upgrade
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> fb6c667b4492, empty message
Traceback (most recent call last):
File "C:\Users\Flashspeed\AppData\Local\Programs\Python\Python35\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
context)
File "C:\Users\Flashspeed\AppData\Local\Programs\Python\Python35\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
cursor.execute(statement, parameters)
psycopg2.ProgrammingError: relation "seq_inbox" does not exist
EDIT 3 Here is the Inbox model itself
class Inbox(db.Model):
__tablename__ = "inbox"
inbox_sequence = db.Sequence(name="seq_inbox", start=1, increment=1, metadata=metadata)
inbox_id = db.Column(db.Integer(), inbox_sequence, server_default=inbox_sequence.next_value(), primary_key=True)
title = db.Column(db.String(60), server_default=missingString, nullable=False)
first_name = db.Column(db.String(30), server_default=missingString, nullable=False)
last_name = db.Column(db.String(30), server_default=missingString, nullable=False)
email = db.Column(db.String(60), nullable=False)
phone_number = db.Column(db.String(60), server_default=missingString, nullable=False)
message = db.Column(db.String(1000), server_default=missingString, nullable=False)