0

I'm using Flask and SQLAlchemy to create a database. But when I add a new column to my database and try to use Flask-Migrate to create the column in my database it shows the error:

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [root] Error: Target database is not up to date.

And I really do not know why? The column I am trying to add is the 'about' column. The database works fine at the moment and doesn't mess up either when I run the web application. I also do not want to drop any tables as they have data which I cannot input again. Can someone please help me?

my models.py is:

from datetime import datetime
from website import db, login_manager
from flask_login import UserMixin
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

followers = db.Table('followers',
    db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
)

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)
    root = db.Column(db.Boolean, nullable=False, default=False)
    about = db.Column(db.Text, default="Welcome to my page!") # I want to add this file but i cannot..
    followed = db.relationship(
        'User', secondary=followers,
        primaryjoin=(followers.c.follower_id == id),
        secondaryjoin=(followers.c.followed_id == id),
        backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    def is_following(self, user):
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    def followed_posts(self):
        return Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id).order_by(
                    Post.date_posted.desc())

    def followed_posts2(self):
        followed = Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id)
        own = Post.query.filter_by(user_id=self.id)
        return followed.union(own).order_by(Post.date_posted.desc())

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}', '{self.root}', '{self.friends}')"

Thank you :D

Blade
  • 190
  • 4
  • 12
  • Your previous upgrade probably failed if you were trying to update the schema of an SQLite DB; this is not supported. Specifically for SQLite you can try setting `migrate.init_app(app, db, render_as_batch=True)`: See the [github issue](https://github.com/miguelgrinberg/Flask-Migrate/issues/61) – roganjosh May 18 '19 at 12:01
  • @roganjosh Even after that, I get the error that that table does not exist, it cannot see the table user.about for some reason? – Blade May 18 '19 at 15:58

0 Answers0