python - creating tables from different models with sqlalchemy -


i have different models e.g. model1.py, model2.py etc. how tables being created following pocoo link, required invoked terminal.

but

    def init_db():         import model.model1         import model.model2         base.metadata.create_all(bind=engine) 

this not working, rather requires invoked terminal.

>> database import init_db >> init_db() #works 

enter image description here


database.py

from sqlalchemy import create_engine sqlalchemy.orm import scoped_session, sessionmaker sqlalchemy.ext.declarative import declarative_base  engine = create_engine('sqlite:///xyz.sqlite', echo=true) db_session = scoped_session(sessionmaker(autocommit=false,                                     autoflush=false,                                     bind=engine)) base = declarative_base() base.query = db_session.query_property()  def init_db():    import model.admin # model.admin import user doesnt either    import model.role    base.metadata.create_all(bind=engine)  if __name__ == '__main__':    init_db() 

admin.py

from sqlalchemy import column, integer, string database import base  class user(base):     __tablename__ = 'users'     id = column(integer, primary_key=true)     name = column(string(50), unique=true)     email = column(string(120), unique=true)      def __init__(self, name=none, email=none):         self.name = name         self.email = email      def __repr__(self):         return '<user %r>' % (self.name) 

there no errors although empty db file generated. how can database created multiple models?

i'm not sure why invoking directly command line triggers table creation i've structured flask apps ala digital ocean's guide. wasn't noted explicitly in quide fact need initialize blueprints first before create_all able build database tables you.

(your code is, lacks blueprints. maybe try create first try again?)


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -