I rechecked everything and I realised that the ForeignKey column is case
sensitive. Thanks for your help!

On 29 July 2016 at 17:58, Blair Azzopardi <blai...@gmail.com> wrote:

> Interesting. Yes each table was in a different schema in pre-simplified
> code and I am using SQL server via pyodbc from Linux. I'll try and get a
> more complete test case shortly.
>
> On 29 Jul 2016 5:53 p.m., "Mike Bayer" <mike...@zzzcomputing.com> wrote:
>
>>
>>
>> On 07/29/2016 12:27 PM, bsdz wrote:
>>
>>> 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:
>>>
>>> |
>>> importsqlalchemy assa
>>> fromsqlalchemy.orm importsessionmaker,relationship
>>> fromsqlalchemy.ext.declarative importdeclarative_base,DeferredReflection
>>>
>>>
>>> Base=declarative_base(cls=DeferredReflection)
>>>
>>> classCountry(Base):
>>>     __tablename__ ='country'
>>>
>>> classUser(Base):
>>>     __tablename__ ='user'
>>>
>>>     country_id =sa.Column(sa.Integer,sa.ForeignKey('country.country_id'))
>>>     country =relationship("Country",uselist=False)
>>>
>>> classMyModel(object):
>>>     def__init__(self,env):
>>>         self._engine =sa.create_engine("...")
>>>         Base.metadata.bind =self._engine
>>>         Base.prepare(self._engine)
>>>
>>>     defcreate_session_maker(self):
>>>         returnsessionmaker(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:Couldnotdetermine join condition between parent/child
>>> tables on relationship User.country -there are noforeign keys linking
>>> these tables.Ensurethat referencing columns are associated witha
>>> ForeignKeyorForeignKeyConstraint,orspecify a 'primaryjoin'expression.
>>>
>>
>> can't reproduce.  Need information on database in use, and if distinct
>> schemas are involved.  See below, and perhaps try to modify this test case
>> in your environment to reproduce your failure.
>>
>>
>> import sqlalchemy as sa
>> from sqlalchemy.orm import sessionmaker, relationship
>> from sqlalchemy.ext.declarative import declarative_base,
>> DeferredReflection
>>
>> e = sa.create_engine("sqlite://", echo=True)
>>
>> e.execute("""
>>     create table country (
>>       country_id integer primary key
>>     )
>> """)
>>
>> e.execute("""
>>     create table user (
>>         user_id integer primary key,
>>         country_id integer,
>>         foreign key (country_id) references country(country_id)
>>     )
>> """)
>>
>>
>> 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 = e
>>         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 = mymodel.create_session_maker()
>> session = Session()
>> l1 = session.query(User).all()
>>
>> print(User.country.property.primaryjoin)
>>
>>
>>
>>
>> |
>>>
>>> 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
>>> <mailto:sqlalchemy+unsubscr...@googlegroups.com>.
>>> To post to this group, send email to sqlalchemy@googlegroups.com
>>> <mailto:sqlalchemy@googlegroups.com>.
>>> Visit this group at https://groups.google.com/group/sqlalchemy.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/sqlalchemy/hZ3NVf1S2cE/unsubscribe.
>> To unsubscribe from this group and all its topics, 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.
>>
>

-- 
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