Search code examples
pythonsqlalchemyflask-sqlalchemyalembicflask-migrate

Is it possible to update the properties of a table with Flask + SQLAlchemy?


I created a table model that has a 'unique=true' property (flask code)

class Books(db.Model):
    id = db.Column(db.Integer, unique=True, primary_key=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    author = db.Column(db.String(255), unique=True, nullable=False)

This was a mistake and I want to change the unique property to false (or just remove it..).

class Books(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    author = db.Column(db.String(255), nullable=False)

How do I re-create the table via flask-python code? Offcourse, I can drop the table and rebuild it with db.create_all(), or do the same via SQL directly on the database, or change it with an SQL command.

Is it possible doing it with the python-sqlalchemy way? (I really like using the python command line) - Preferably just with a small edit without dropping the data in the table.


Solution

  • yes it's possible.
    you need to learn migration: Flask Migration Example