Flask-SQLAlchemy + create_all() failed on many-to-many relationship creation -



Flask-SQLAlchemy + create_all() failed on many-to-many relationship creation -

i have next application structure:

run.py |->app |->models |-> user.py (declares role-to-user relationship table , user model) |-> role.py (declares role model) |-> main.py (contains initialization , required imports) |-> extensions (here sqlalchemy variable declared imported later)

when trying create initial db construction next way:

from app.models import * app.extensions import db app.main import myapp #app initialized packages assets, db, security, etc. myapp.test_request_context(): db.create_all()

i've got exception: noreferencedtableerror: foreign key associated column 'users_to_roles.user_id' not find table 'users' generate foreign key target column 'id'

i tried dow next way:

@app.before_first_request def initialize_database(): db.create_all()

with no success

i tried move role same file user model declared same result. read docs here: https://pythonhosted.org/flask-sqlalchemy/quickstart.html says 'you need import db' , not works.

here how user model , relationship table declared (role model looks similar user):

users_to_roles_association_table = db.table('users_to_roles', db.column('user_id', db.integer, db.foreignkey('users.id')), db.column('role_id', db.integer, db.foreignkey('roles.id'))) class user(db.model, usermixin): id = db.column(db.integer, primary_key=true) email = db.column(db.string(255), unique=true) ....... roles = db.relationship('role', secondary=users_to_roles_association_table, backref=db.backref('users', lazy='dynamic'))

how create initial db construction using flask-sqlalchemy?

one of features of flask-sqlalchemy automatic table name generation class name when __tablename__ class attribute isn't defined model class. https://pythonhosted.org/flask-sqlalchemy/models.html:

some parts required in sqlalchemy optional in flask-sqlalchemy. instance table name automatically set unless overridden. it’s derived class name converted lowercase , “camelcase” converted “camel_case”.

in case, user class have it's __tablename__ attribute inferred user means there no users table defined.

you can prepare either explicitly setting __tablename__ or changing foreignkey parameters match autogenerated table names:

class="lang-py prettyprint-override">class user(db.model, usermixin): __tablename__ = 'users' ... class role(db.model): __tablename__ = 'roles' ...

or leave __tablename__ attribute undefined , modify association table:

class="lang-py prettyprint-override">users_to_roles_association_table = db.table('users_to_roles', db.column('user_id', db.integer, db.foreignkey('user.id')), db.column('role_id', db.integer, db.foreignkey('role.id')))

flask flask-sqlalchemy

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -