in Python, this is called a circular module import:

lattice_definition.py: 
from Irmis.model_definition import model

model_definition.py:
from Irmis.lattice_definition import Base
from Irmis.lattice_definition import lattice

both modules are dependent on each other:

lattice_definition <--->  model_definition


normally, the simplest solution here is to create a third module that contains 
the elements that are common to both lattice_definition.py and 
model_definition.py so that they are not mutually dependent on each other:

meta.py
contains Base, model

lattice_definition.py: 
from meta import model

model_definition.py:
from meta import Base
from Irmis.lattice_definition import lattice


that way the dependency chain does not contain loops, i.e.   lattice_definition 
-> (meta),  model_definition -> (lattice_definition, meta)



On Feb 15, 2013, at 11:32 AM, "Arkilic, Arman" <[email protected]> wrote:

> Hi, 
> I am quite new to sqlalchemy. I am trying to implement a many-to-one 
> relationship between classes in different modules. I was able to get this to 
> work when classes are inside the same module however when I put the classes 
> in different modules I get import errors. I made sure I used strings while 
> referring to the keys. I was wondering if someone can give me some advice. 
> lattice_definition.py: 
> from sqlalchemy.orm import sessionmaker, relationship
> from setuptools.tests.test_resources import Metadata
> from sqlalchemy.orm import relationship,backref
> from sqlalchemy import ForeignKey
> from sqlalchemy.schema import PrimaryKeyConstraint
> from Irmis.model_definition import model
> 
> 
> class lattice(Base):
>     __tablename__='lattice'    
>     lattice_id= Column(Integer, primary_key=True)
>     machine_mode_id=Column(Integer,ForeignKey('machine_mode.machine_mode_id'))
>     
> model_geometry_id=Column(Integer,ForeignKey('model_geometry.model_geometry_id'))
>     model_line_id=Column(Integer,ForeignKey('model_line.model_line_id'))    
>     gold_lattices=relationship("gold_lattice")
>     elements=relationship("element")
> model_definition.py:
> 
> from sqlalchemy.orm import relationship,backref
> from sqlalchemy import ForeignKey
> from sqlalchemy.schema import PrimaryKeyConstraint
> from Irmis.lattice_definition import Base
> from Irmis.lattice_definition import lattice
> 
> class model(Base):
>     __tablename__='model'
>     model_id=Column(Integer,primary_key=True)
>     lattice_id=Column(Integer,ForeignKey("lattice.lattice_id"))
> 
> Thanks,
> --Arman
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to