我试图使用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)]
这清楚地表明反射拾取了外键.
我哪里错了?
解决方法:
在用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
案件结案.