0

When I try to use flask-migrate with my model and a Postgres database, it does not work. Even if the database is created at startup. Every time migrate only detects the following:

INFO  [alembic.autogenerate.compare] Detected added unique constraint 'None' on '['Auth_ID']'
INFO  [alembic.autogenerate.compare] Detected added unique constraint 'None' on '['ClosedCourse_ID']'

My Model (much larger but is poorly designed like this)

class SKAuthentication(db.Model):
    __tablename__ = 'sk-authentication'

    Auth_ID = db.Column(UUID(as_uuid=True), primary_key=True, unique=True)
    Basic_Auth = db.Column(Text, nullable=False)
    Basic_User = db.Column(Text, nullable=False)
    Teacher_ID = db.Column(UUID(as_uuid=True), db.ForeignKey('sk-teacher.Teacher_ID'), nullable=False)
    Model = db.Column(Text, nullable=True)
    Phone_ID = db.Column(Text, nullable=True)
    Brand = db.Column(Text, nullable=True)
    VersionInstalled = db.Column(Text, nullable=False, default='0.0.0')

    def __init__(self, teacher_id):
        chars = string.digits + string.ascii_letters + string.punctuation
        self.Basic_Password = ''.join(secrets.choice(chars) for _ in range(256))
        self.Auth_ID = create_ID(teacher_id, token_1, auth_crypt)
        self.Basic_Auth = sha512_crypt.encrypt(self.Basic_Password)
        self.Basic_User = create_ID(self.Basic_Auth, self.Auth_Secret)
        self.Teacher_ID = teacher_id


class SKDayilyClosedCourse(db.Model):
    __tablename__ = 'sk-daily-closed-course'

    ClosedCourse_ID = db.Column(UUID(as_uuid=True), primary_key=True, unique=True)
    Teachers_Group_ID = db.Column(UUID(as_uuid=True), db.ForeignKey('sk-teachers-groups.Row_ID'), nullable=False)
    Course_Date = db.Column(Date, nullable=False)
    Closed = db.Column(BOOLEAN, nullable=False, default=False)
    Reminded = db.Column(BOOLEAN, nullable=False, default=False)

    def __init__(self, teachers_group_id, course_date, reminded):
        self.ClosedCourse_ID = create_ID(teachers_group_id, False, False, course_date)
        self.Teachers_Group_ID = teachers_group_id
        self.Course_Date = course_date
        self.Reminded = reminded

My run.py looks like this:

from flask import Flask, session
from flask_sqlalchemy import SQLAlchemy
from modules.extensions import csrf, migr, cors
from config import DevelopmentConfig
from flask_migrate import upgrade, migrate

db = SQLAlchemy()
migr = Migrate()

def create_app():
    app = Flask(__name__)
    csrf.init_app(app)
    app.config.from_object(DevelopmentConfig)
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    cors.init_app(app, resources={r"/mobile/v1/*": {"origins": "GET, POST, OPTIONS"}})
    migr.init_app(app, db, render_as_batch=False)

    with app.app_context():
        import modules.database.model
        db.create_all()
        db.session.commit()
        migrate()
        upgrade()

        from application import application
        from application import application_ticket
        from application import application_mobile
        from application import application_bookings

        app.register_blueprint(application.application)
        app.register_blueprint(application_ticket.ticket, url_prefix="/ticket")
        app.register_blueprint(application_mobile.mobile, url_prefix="/mobile/v1")
        app.register_blueprint(application_bookings.bookings, url_prefix="/bookings/v1")

       
    @app.before_request
    def before_request():
        session.permanent = True
        app.permanent_session_lifetime = timedelta(minutes=1)

    return app

This is the important part form my extensions.py

from flask_wtf import CSRFProtect
from flask_migrate import Migrate
from flask_cors import CORS

csrf = CSRFProtect()
migr = Migrate()
cors = CORS()
...

And that's my wsgi.py:

from run import create_app

app = create_app()

Once it worked, but I don't know how. I didn't change anything, so I tried to reproduce it and I encountered the same problem I mentioned before. I am not sure how to proceed with this problem. I tried to create the tables inside the models.py and try to migrate everything from there, but still just the unique constraints. Another way, I figured out was to import the models, which I also needed to create the tables with Flask-SQLAlchemy, but that changed nothing. Next solution I've found was, to run everything from console. Nothing changed. Other solution: Drop Table "alembic_version" -> didn't changed anything.

Hopefully someone can help me soon!

I am expecting to get following output:

INFO  [alembic.autogenerate.compare] Detected added table '[<Tablename>]'
...

Thank you all!

  • For the tables to be detected as new tables they have to not be in the database. It seems you are doing this backwards, your database should be completely empty, and then Alembic will write a migration to add the tables. – Miguel Grinberg Nov 30 '22 at 10:54
  • Thank you @MiguelGrinberg If I've got enough time for it, I'm going to test it. But for now, we have a custom created update script, which is detecting existing tables etc. – mofukommit Dec 05 '22 at 12:08

0 Answers0