vendredi 13 février 2015

NameError: name 'Base' is not defined

Following is my file having all Table mapper classes.



// import statements
Class MyBase(AbstractBase, Base):
session = None
def __init__(self):
global Session
self.session = Session()
// init statements

Class Table1(MyBase):
// fields

engine = create_engine("...")

Session = session_maker(engine)

Base = declarative_base()

Base.meta.create_all()




  1. I am getting NameError: name 'Base' is not defined since my class is extending it before Base initialization.




  2. I can make it work, if I move engine creation, Session creation & Base creation above defining classes. But Base.meta.create_all() should be present below the class declaration. If I place Base.meta.create_all() above classes, there is no table created.




  3. I do not want to have the main() because, I am basically going to import this file in some other file.




So, I want to keep all statements(engine, session, base & create_all()) after the class definitions.


Aucun commentaire:

Enregistrer un commentaire