Sqlalchemy ORM 表之间的非声明性链接

发布于 2024-12-04 19:52:08 字数 1438 浏览 2 评论 0原文

我正在使用 SQLAlchemy,但不是以声明方式,我本质上是尝试使用 SOAP Web 服务(动态类)提供的数据来填充数据库。

我遇到的麻烦是在对象的不同部分之间创建映射,由于这些表位于 MySQL 的 MyISAM 表上,而该表没有外键支持,因此这变得更加困难。

import sqlalchemy

dbE = sqlalchemy.create_engine('create string')
dbM = sqlalchemy.MetaData()
dbM.bind = dbE

class Invoice(object):
    pass

class LineItem(object):
    pass

InvoiceTbl = sqlalchemy.Table('Invoices', dbM, autoload = True)
LineItemTbl = sqlalchemy.Table('Invoices_Items', dbM, autoload = True)

LineItemMapper = orm.mapper(InvoiceLineItemTbl, InvoiceLineItemTbl)
InvoiceMapper = orm.mapper(Invoice, InvoiceTbl)

sm = orm.sessionmaker(bind = dbE, autoflush = True, autocommit = False, 
                      expire_on_commit = True)
session = orm.scoped_session(sm)

使用上面的内容,我可以使用以下内容分别查询发票的不同部分,对象将填充匹配表中存在的列:

invoiceData = session.query(Invoice).filter(Invoice.InvoiceNumber.like('12345'))
invoiceItems = session.query(LineItem).filter(LineItem.InvoiceNumber.like('12345'))

我想要做的是将 LineItems 作为发票的一部分:

orm.mapper(Invoice, InvoiceTbl properties={
    'LineItems' : orm.relation(LineItem),
    })

但这又回来了出现错误,它不知道两个对象/表之间的关系(考虑到不存在外键,这是可以理解的)。我一直在尝试不同的参数(primaryjoin、backref 等),但到目前为止我还没有成功。

任何帮助将不胜感激,我知道这可能不是最好的方法,但这个特定的脚本是数据驱动的,并且需要在 SOAP 服务发生变化时保持一定的动态性。

编辑: shahjapan 对primaryjoin 参数的建议有所帮助,但我现在收到错误“无法确定primaryjoin 条件的关系方向”,所以我认为我们正走在正确的轨道上,只是还没有到达。

I am using SQLAlchemy but not in the declarative manner, I essentially am trying to populate a database using data being fed by a SOAP web service (dynamic classes).

What I am having trouble with is creating the mappings between the different parts of the object, this is being made more difficult by the fact that the tables are on MySQL's MyISAM tables which don't have foreign key support.

import sqlalchemy

dbE = sqlalchemy.create_engine('create string')
dbM = sqlalchemy.MetaData()
dbM.bind = dbE

class Invoice(object):
    pass

class LineItem(object):
    pass

InvoiceTbl = sqlalchemy.Table('Invoices', dbM, autoload = True)
LineItemTbl = sqlalchemy.Table('Invoices_Items', dbM, autoload = True)

LineItemMapper = orm.mapper(InvoiceLineItemTbl, InvoiceLineItemTbl)
InvoiceMapper = orm.mapper(Invoice, InvoiceTbl)

sm = orm.sessionmaker(bind = dbE, autoflush = True, autocommit = False, 
                      expire_on_commit = True)
session = orm.scoped_session(sm)

Using the above I can query for the different parts of an invoice separately with the following, the objects get populated with the columns present in the matching table:

invoiceData = session.query(Invoice).filter(Invoice.InvoiceNumber.like('12345'))
invoiceItems = session.query(LineItem).filter(LineItem.InvoiceNumber.like('12345'))

What I want to do is make the LineItems as part of the Invoice:

orm.mapper(Invoice, InvoiceTbl properties={
    'LineItems' : orm.relation(LineItem),
    })

But this comes back with an error about it doesn't know about the relation between the two objects / tables (understandable given that no foreign keys exist). I've been trying different parameters (primaryjoin, backref, etc) but I have not been successful thus far.

Any help would be appreciated, I understand this may not be the best way to do this but this particular script is data driven and needs to be somewhat dynamic in the event the SOAP service changes.

edit:
shahjapan's suggestion of primaryjoin argument helps but I am now getting an error "Could not determine relationship direction for primaryjoin condition" so I assume we are on the right track, just not there yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-12-11 19:52:08

只要给孩子外键,它就会自动工作。

行项目

LineItemTbl = sqlalchemy.Table('Invoices_Items', dbM,
    Column(InvoiceNumber, ForeignKey('InvoiceTbl.InvoiceNumber),
    autoload = True)

Just give foreign key in the child and it will work authomaticaly.

LineItme

LineItemTbl = sqlalchemy.Table('Invoices_Items', dbM,
    Column(InvoiceNumber, ForeignKey('InvoiceTbl.InvoiceNumber),
    autoload = True)
绻影浮沉 2024-12-11 19:52:08

尝试使用 relation API 的 PrimaryJoin 关键字参数,

orm.mapper(Invoice, InvoiceTbl properties={
    'LineItems' : orm.relation(LineItem,
                  primaryjoin='Table1.id==Table2.refid'),
    })

Try using PrimaryJoin keyword arg of relation API,

orm.mapper(Invoice, InvoiceTbl properties={
    'LineItems' : orm.relation(LineItem,
                  primaryjoin='Table1.id==Table2.refid'),
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文