SQLAlchemy:使用声明式更新的更好方法?

发布于 2024-08-29 10:15:52 字数 419 浏览 5 评论 0原文

假设我有一个处于声明模式的用户表:

class User(Base):
    __tablename__ = 'user'
    id = Column(u'id', Integer(), primary_key=True)
    name = Column(u'name', String(50))

当我知道用户的 id 而没有将对象加载到会话中时,我会像这样更新这样的用户:

ex = update(User.__table__).where(User.id==123).values(name=u"Bob Marley")
Session.execute(ex)

我不喜欢使用 User.__table__,我应该停止担心吗?

有更好的方法吗?

Let's say I have a user table in declarative mode:

class User(Base):
    __tablename__ = 'user'
    id = Column(u'id', Integer(), primary_key=True)
    name = Column(u'name', String(50))

When I know user's id without object loaded into session, I update such user like this:

ex = update(User.__table__).where(User.id==123).values(name=u"Bob Marley")
Session.execute(ex)

I dislike using User.__table__, should I stop worrying with that?

Is there a better way to do this?

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

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

发布评论

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

评论(3

酒中人 2024-09-05 10:15:53

ORM 级别还有一些更新功能。它还不能处理任何棘手的情况,但对于单行更新(或批量更新)的琐碎情况,它工作得很好。它甚至会检查任何已加载的对象并对它们应用更新。你可以这样使用它:

session.query(User).filter_by(id=123).update({"name": u"Bob Marley"})

There's also some update capability at the ORM level. It doesn't handle any tricky cases yet but for the trivial case of single row update (or bulk update) it works fine. It even goes over any already loaded objects and applies the update on them also. You can use it like this:

session.query(User).filter_by(id=123).update({"name": u"Bob Marley"})
新人笑 2024-09-05 10:15:53

您在这里是在子句级别上工作,而不是在模型/实体/对象级别上工作。子句级别低于映射对象。是的,必须采取一些措施将一种术语转换为其他术语。

您也可以停留在对象级别并执行以下操作:

session = Session()
u = session.query(User).get(123)
u.name = u"Bob Marley"
session.commit()

但它会明显变慢,因为它会导致映射对象构造。我不确定它是否更具可读性。

在您提供的示例中,我看到了最自然和“正确”的解决方案。我不会担心 __table__ 的小魔法。

You're working on clause level here, not on model/entity/object level. Clause level is lower than mapped objects. And yes, something have to be done to convert one terms into others.

You could also stay on object level and do:

session = Session()
u = session.query(User).get(123)
u.name = u"Bob Marley"
session.commit()

but it will be significantly slower since it leads to the mapped object construction. And I'm not sure that it is more readable.

In the example your provided I see the most natural and “right” solution. I would not worry about little __table__ magic.

梦纸 2024-09-05 10:15:53

通过 Table 对象上的 update() 方法可以使用类似的功能。

class User(Base):
    __tablename__   = 'user'
    id = Column('id', Integer(), primary_key=True)
    name = Column('name', String(50))

stmt = User.__table__.update().where(User.id==5).values(name='user #5')

在 SQLAlchemy 中使用 User.__table__ 是如何完成的。

Similar functionality is available via the update() method on Table object.

class User(Base):
    __tablename__   = 'user'
    id = Column('id', Integer(), primary_key=True)
    name = Column('name', String(50))

stmt = User.__table__.update().where(User.id==5).values(name='user #5')

To use User.__table__ is how its done in SQLAlchemy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文