masetto wrote:
> Hi all,
>
> i am new to SQLAlchemy (simply wonderful!), and i'm writing some
> python scripts to do some experiment.
>
> I've written a SINGLE python module with two classes which define two
> different tables (declarative_base) with a simple relationship and a
> single Foreign Key and everything WORKS fine as expected. Cool! :P
>
> Then, i've moved that two classes in two different python modules to
> better organize my code but now i got the following error:
> sqlalchemy.exc.NoReferencedTableError: Could not find table 'metadata'
> with which to generate a foreign key
>
> I understood the error message but i can't found a way to resolve this
> issue, can you help me?
>
> This is my "directory layout":'


you need to share one declarative "Base" class for all of the classes that
are related to each other, or alternatively at least a single "MetaData"
for all tables that wish to reference foreign keys using strings.    
Anytime you specify options using strings to find something else, the
relevant "base" has to be shared, i.e. declarative base if using strings
in "relation()", and MetaData if using strings in ForeignKey().







>
> test.py - the main python module
> SQLTableBuilder_Definition.py - class that define a table called
> "Definition"
> SQLTableBuilder_Metadata.py - class that define a table called
> "Metadata"
>
> test.py:
> ...
> engine = create_engine('sqlite:///test.db3', echo=True,
> encoding='utf-8' )
>
> import SQLTableBuilder_Metadata
>
> metadata_table = SQLTableBuilder_Metadata.MetadataClass.__table__
> metadata = SQLTableBuilder_Metadata.Base.metadata
> metadata.create_all(engine)
>
> import SQLTableBuilder_Definition
> definitions_table =
> SQLTableBuilder_Definition.DefinitionClass.__table__
> metadata = SQLTableBuilder_Definition.Base.metadata
> metadata.create_all(engine)  <== My script explode here with the
> following:
>
> Traceback (most recent call last):
>   File "test.py", line 82, in <module>
>     metadata.create_all(engine)
>     ...
>     sqlalchemy.exc.NoReferencedTableError: Could not find table
> 'metadata' with which to generate a foreign key
>
>
> Session = sessionmaker(bind=engine)
> Session.configure(bind=engine)
> session = Session()
>
> ...
> session.add(myObj)
> ...
>
> SQLTableBuilder_Definition.py:
>
> Base = declarative_base()
>
> class DefinitionClass(Base):
>     __tablename__ = 'definitions'
>
>     defId = Column(Integer, primary_key=True)
>     Id = Column(String)
>     classType = Column(String)
>     version = Column(String)
>
>     metadataId = Column('metadataId', Integer,
> ForeignKey('metadata.metadataId'))
>
>     def __init__(self, node):
>         self.Id = node.get("id")[len(IdName):]
>         self.version = node.get("version")
>         self.classType = node.get("class")
>
> SQLTableBuilder_Metadata.py:
> import SQLTableBuilder_Definition
>
> Base = declarative_base()
>
> class MetadataClass(Base):
>     __tablename__ = 'metadata'
>
>     metadataId = Column(Integer, primary_key=True)
>     title = Column(String)
>
>     defRef = relation(SQLTableBuilder_Definition.DefinitionClass,
> backref="metadata")
>
>     def __init__(self, node):
>         self.title = node
>
> If i remove relation and foreign key from the two classes, everything
> works fine again.
> I suppose that python can't find the metadata table (previously
> created without error) from SQLTableBuilder_Definition.py, but how i
> can "point him" in the right direction?
>
> Thanks for your attention.
>
> ps.
>>>> print sqlalchemy.__version__
> 0.6beta1
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to