orm - Python - Class references another class before assignment -


i working sqlalchemy's orm create classes mapped sql tables. running issues generating relationships between these classes since reference each other before class declared. when run code interpreter complains nameerror: name 'account' not defined

i've included code sample below demonstrates how declaring these classes.

class location(base):     __tablename__ = 'locations'     id = column(integer, primary_key=true)     name = column(string)     address = column(string)     city = column(string)     state = column(string)     zip_code = column(string)      account = sa.orm.relationship('account', order_by=account.id, back_populates='location')     entity = sa.orm.relationship('entity', order_by=entity.id, back_populates='location')      def __repr__(self):         return "<location(name='{}', address='{}', city='{}', state='{}', zip_code='{}')>".\                     format(self.name, self.address, self.city, self.state, self.zip_code)   class account(base):     __tablename__ = 'accounts'     id = column(integer, primary_key=true)     name = column(string)     number = column(string)     institution = column(string)     # entity_id   = column(integer, sa.foreignkey('entities.id'))      entity = sa.orm.relationship('entity', back_populates='accounts')     location = sa.orm.relationship('location', order_by=location.id, back_populates='account')      def __repr__(self):         return "<account(name='{}', account={}, institution={}, entity={})>".\                 format(self.name, self.number, self.institution, self.entity)   class entity(base):     __tablename__ = 'entities'     id = column(integer, primary_key=true)     name = column(string)      accounts = sa.orm.relationship('account', order_by=account.id, back_populates='entity')     location = sa.orm.relationship('location', order_by=location.id, back_populates='entity')      def __repr__(self):         return "<entity(name='{}', location='{}')>".format(self.name, self.location) 

what missing here? there way define classes , call them later can functions? example functions, it's simple call main @ bottom after functions defined:

def main():     foo()  def foo():  if __name__=='__main__':     main() 

define orderings either callables or expression strings, explained in relationship api documentation:

class location(base):     ...     account = sa.orm.relationship('account',         order_by=lambda: account.id, ...) 

or

class location(base):     ...     account = sa.orm.relationship('account',         order_by='account.id', ...) 

the problem during evaluation of location class' body name account not yet exist in global scope, , not defined in local scope of class body. passing in function/lambda allows deferring evaluation "mapper initialization time":

some arguments accepted relationship() optionally accept callable function, when called produces desired value. callable invoked parent mapper @ “mapper initialization” time, happens when mappers first used, , assumed after mappings have been constructed. can used resolve order-of-declaration , other dependency issues, such if child declared below parent in same file

passing string resolve order-of-declaration issue, , provides feature:

these string arguments converted callables evaluate string python code, using declarative class-registry namespace. allows lookup of related classes automatic via string name, , removes need import related classes @ local module space


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -