0

i've an automap to work with an existing database in my app project. When i start it on my pc with flask run command, it works fine, and it saves data on the database. Now i'm trying to put my app on a debian server, when i run the app with gunicorn, it return me this error:

sqlalchemy.exc.ArgumentError: Mapper mapped class Applications->applications could not assemble any primary key columns for mapped table 'applications'.

This is my models.py:

from flask_login import UserMixin
from sqlalchemy import Column, Date, Integer, String, create_engine
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.automap import automap_base

Base = automap_base()

class User(UserMixin, Base, db.Model):
    __tablename__ = "accounts"

    def __repr__(self):
        return "User {}".format(self.acc_username)

    def get_id(self):
           return (self.acc_id)

class Char(Base, db.Model):
    __tablename__ = "characters"

    def __repr__(self):
        return self.char_name.replace('_',' ')

class Applications(Base, db.Model):
    __tablename__="applications"

    def __repr__(self):
        return "Application ".format(self.id)

Base.prepare(db.engine, reflect=True)

@login.user_loader
def load_user(id):
    return User.query.get(int(id))

I've already tried to specify the primary key column into the classes, but after this, it does not find the other parameters, so it is as if it wanted me to write them one by one, as if the automap from the existing database was not working

This is the db declaration:

db = SQLAlchemy(app)

Database config:

SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'app.db')

Database URL:

DATABASE_URL=mysql+pymysql://root:xxxx@localhost:3306/xxxx
kocis
  • 9
  • 5

1 Answers1

0

Without declaring your columns, keys and indexes in the codebase it might lead to such cases if you are switching from SQLLite to MySQL or from local to server for example (at least from my previous experience)

In your case I would do one of the following things:

  1. Apply better automap functionality as the documentation states (maybe with reflections): https://docs.sqlalchemy.org/en/13/orm/extensions/automap.html

  2. reverse engineer the db structure, keys and relations into models by hand or by some tools like sqlacodegen url: https://pypi.org/project/sqlacodegen/

  3. My preference: Go with flask migrate for better convenience and describe all your models, keys, relationships, indexes in the code url: https://flask-migrate.readthedocs.io/en/latest/ Describing your models better and at full as well as start migrations from flask side, that is the sane way to orchestrate and have good results every time.