2

Using Flask-Migrate and SQLite3, I am trying to alter column 'confirmed' in table 'user' so that becomes NOT NULLABLE and has a default value of false. I have updated the existing rows so that confirmed = false for all of the entries. Though when I try to update the table so that default = False then I receive the following error:

[SQL: ALTER TABLE user ALTER COLUMN confirmed SET NOT NULL]
(Background on this error at: http://sqlalche.me/e/13/e3q8)

Schema of the table

CREATE TABLE user (
        id INTEGER NOT NULL, 
        username VARCHAR(20) NOT NULL, 
        email VARCHAR(120) NOT NULL, 
        image_file VARCHAR(20) NOT NULL, 
        password VARCHAR(60) NOT NULL, confirmed BOOLEAN, confirmed_on DATETIME, 
        PRIMARY KEY (id), 
        UNIQUE (email), 
        UNIQUE (username)

I have noticed that the latest migrate file from Flask-Migrate does not include the default = false attribute, even though it should because I have added the code to models.py:

Models.py:

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)
    batches = db.relationship('Batch', backref='author', lazy=True)
    comments = db.relationship('Feedback', backref='author', lazy=True)

    #New columns
    confirmed = db.Column(db.Boolean, nullable=False, default=False)
    confirmed_on = db.Column(db.DateTime, nullable=True)

This is the migration file generated by flask-migrate:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('user', 'confirmed',
               existing_type=sa.BOOLEAN(),
               nullable=False)

However, when I manually edit this file so that it includes default = False, i.e.:

op.alter_column('user', 'confirmed',
               existing_type=sa.BOOLEAN(),
               nullable=False, default=False)

Then I still receive the same error. (I have also tried editing the migrate file so that the default=false change is a new entry).

Hopefully that makes sense, I am confused and feel like I have hit a wall.

Ambassador Kosh
  • 459
  • 5
  • 19
  • That's not alter table syntax that sqlite understands. – Shawn Mar 25 '21 at 22:30
  • @Shawn but it is what is being generated by Flask-SQLAlchemy? I am not sure what to do. – Ambassador Kosh Mar 29 '21 at 13:29
  • No idea; I try to not touch ORMs with a 10-foot pole. The normal workflow for making a change to a table definition beyond sqlite's limited `ALTER TABLE` features is documented here: https://www.sqlite.org/lang_altertable.html – Shawn Apr 01 '21 at 13:12

0 Answers0