I am attempting to use SqlAlchemy (0.5.8) to interface with a legacy database declaratively and using reflection. My test code looks like this:
from sqlalchemy import *
from sqlalchemy.orm import create_session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('oracle://schemaname:pwd@SID')
meta = MetaData(bind=engine)
class CONSTRUCT(Base):
__table__ = Table('CONSTRUCT', meta, autoload=True)
class EXPRESSION(Base):
__table__ = Table('EXPRESSION', meta, autoload=True)
session = create_session(bind=engine)
Now when I attempt to run a query using the join between these two tables (defined by a foreign key constraint in the underlying oracle schema):
print session.query(EXPRESSION).join(PURIFICATION)
... no joy:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'EXPRESSION' and 'PURIFICATION'
However:
>>> EXPRESSION.epiconstruct_pkey.property.columns
[Column(u'epiconstruct_pkey', OracleNumeric(precision=10, scale=2, asdecimal=True,
length=None), ForeignKey(u'construct.pkey'), table=<EXPRESSION>, nullable=False)]
>>> CONSTRUCT.pkey.property.columns
[Column(u'pkey', OracleNumeric(precision=38, scale=0, asdecimal=True, length=None),
table=<CONSTRUCT>, primary_key=True, nullable=False)]
Which clearly indicates that the reflection picked up the foreign key.
Where am I going wrong?