我正在尝试使用SqlAlchemy(0.5.8)以声明方式和使用反射来与旧数据库进行交互。我的测试代码如下所示:
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)
现在,当我尝试使用这两个表之间的连接(由底层oracle架构中的外键约束定义)运行查询时:
print session.query(EXPRESSION).join(PURIFICATION)
......没有快乐:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'EXPRESSION' and 'PURIFICATION'
然而:
>>> 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)]
这清楚地表明反射拾取了外键。
我哪里错了?
答案 0 :(得分:5)
使用Eclipse调试脚本+ SqlAlchemy代码后,我发现表/列的列表在内部保留为小写。因此,EXPRESSION.foreignkey和expression.foreignkey之间永远不可能匹配。因此错误信息。
深入研究SqlAlchemy文档(http://www.sqlalchemy.org/docs/reference/dialects/oracle.html#identifier-casing)然后我发现了以下内容:
“在Oracle中,数据字典使用UPPERCASE文本表示所有不区分大小写的标识符名称。另一方面,SQLAlchemy认为全小写标识符名称不区分大小写.Oracle方言将所有不区分大小写的标识符转换为模式级通信期间的两种格式,例如表和索引的反射。在SQLAlchemy端使用UPPERCASE名称表示区分大小写的标识符,SQLAlchemy将引用名称 - 这将导致与从Oracle收到的数据字典数据不匹配,所以除非标识符名称已真正创建为区分大小写(即使用带引号的名称),所有小写名称都应在SQLAlchemy端使用。“
所以我的代码看起来像这样(差异只是大小写改变):
from sqlalchemy import *
from sqlalchemy.orm import create_session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('oracle://EPIGENETICS:sgc04lab@ELN')
meta = MetaData(bind=engine)
class construct(Base):
__table__ = Table('construct', meta, autoload=True)
class expression(Base):
__table__ = Table('expression', meta, autoload=True)
class purification(Base):
__table__ = Table('purification', meta, autoload=True)
session = create_session(bind=engine)
print session.query(expression).join(purification,expression)
......吐出来的:
SELECT expression.pkey AS expression_pkey, expression.cellline AS expression_cellline, expression.epiconstruct_pkey AS expression_epiconstruct_pkey, expression.elnexp AS expression_elnexp, expression.expression_id AS expression_expression_id, expression.expressioncomments AS expression_expressioncomments, expression.cellmass AS expression_cellmass, expression.datestamp AS expression_datestamp, expression.person AS expression_person, expression.soluble AS expression_soluble, expression.semet AS expression_semet, expression.scale AS expression_scale, expression.purtest AS expression_purtest, expression.nmrlabelled AS expression_nmrlabelled, expression.yield AS expression_yield
FROM expression JOIN purification ON expression.pkey = purification.epiexpression_pkey JOIN expression ON expression.pkey = purification.epiexpression_pkey
案件结案。