Hi

I'm trying to use DeferredReflection to encapsulate my data model so that 
it can easily be instantiated for different environments. However, I am 
having trouble creating relationships with a NoForeignKeysError being 
raised. I am guessing it is because the table metadata generation is being 
deferred, there's not enough information to create the foreign key 
information and therefore the relationships.

I've simplified my code to the following:

import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection


Base = declarative_base(cls=DeferredReflection)
  
class Country(Base):
    __tablename__ = 'country'
    
class User(Base):
    __tablename__ = 'user'
    
    country_id = sa.Column(sa.Integer, sa.ForeignKey('country.country_id'))
    country = relationship("Country", uselist=False)
        
class MyModel(object):
    def __init__(self, env):
        self._engine = sa.create_engine("...")
        Base.metadata.bind = self._engine
        Base.prepare(self._engine)
    
    def create_session_maker(self):
        return sessionmaker(bind=self._engine)
 
# This code is run in another module.
mymodel = MyModel("DEV")
Session = model.create_session_maker()
session = Session()
l1 = session.query(User).all() 

The following error comes back: 

NoForeignKeysError: Could not determine join condition between parent/child 
tables on relationship User.country - there are no foreign keys linking these 
tables. Ensure that referencing columns are associated with a ForeignKey or 
ForeignKeyConstraint, or specify a 'primaryjoin' expression.

Any suggestions on how I might get around this?


Thanks in advance,
Blair


-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to