如何使用 OpenERP 在 Postgresql 中获取最后插入的 id

发布于 2024-11-04 07:35:53 字数 187 浏览 0 评论 0 原文

我有一个插入查询,我想获取 OpenERP 中最后插入的 id。代码如下:

query = "INSERT INTO foo SELECT * FROM bar"
cr.execute(query) # cr => cursor

如何获取最后插入的 id(s)?当插入为空时发生了什么?

I have a insert query and I want to get last inserted id in OpenERP. Here is the code:

query = "INSERT INTO foo SELECT * FROM bar"
cr.execute(query) # cr => cursor

How to get last inserted id(s)? What happened when insertion is empty?

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

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

发布评论

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

评论(4

鸢与 2024-11-11 07:35:54

查看 RETURNING 子句

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
    [ RETURNING * | output_expression [ AS output_name ] [, ...] ]

向表分配器中插入一行,返回由 DEFAULT 子句生成的序列号:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;

Look at the RETURNING clause.

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
    [ RETURNING * | output_expression [ AS output_name ] [, ...] ]

Insert a single row into table distributors, returning the sequence number generated by the DEFAULT clause:

INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;
一张白纸 2024-11-11 07:35:54

如果您运行的是 v8.2+,RETURNING 效果很好。否则,您可能会考虑使用 currval() (此处为文档)。

RETURNING works great if you're running v8.2+. Otherwise you'll probably be looking at using currval() (doc here).

‘画卷フ 2024-11-11 07:35:54

看到 openerp 标签的链接,我不得不建议您使用 openerp orm。对于调用create方法的orm将返回新创建记录的id。 (它也适用于您可能定义的任何 osv_memory 类)

参考: http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html#osv.osv.osv.create

示例:

new_id = self.pool.get('model.name').create(cr, uid, {'name': 'New Name'})

Seeing the link to openerp tag, I'd have to advise you to use the openerp orm. For the orm calling the create method will return the id of the newly created record. (It also then works for any osv_memory classes you may have defined)

Reference: http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html#osv.osv.osv.create

Example:

new_id = self.pool.get('model.name').create(cr, uid, {'name': 'New Name'})
人事已非 2024-11-11 07:35:54

看起来像Sentinel的建议 PostgreSQL RETURNING 子句 是最容易使用的。

您可能还对 OpenERP 的 ORM 类如何管理新记录的 id 值感兴趣。这是 的片段orm.create() 方法:

    # Try-except added to filter the creation of those records whose filds are readonly.
    # Example : any dashboard which has all the fields readonly.(due to Views(database views))
    try:
        cr.execute("SELECT nextval('"+self._sequence+"')")
    except:
        raise except_orm(_('UserError'),
                    _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
    id_new = cr.fetchone()[0]

它使用每个表的序列来生成新的 id 值。序列的名称默认为表名称加“_id_seq”,如您在 orm.__init__() 方法

我不知道您想要完成什么,但您可能会发现使用 orm 为您创建记录并让它处理详细信息。例如,创建方法 返回新记录的 id 值。

It looks like Sentinel's suggestion of the PostgreSQL RETURNING clause is the easiest thing for you to use.

You might also be interested in how OpenERP's ORM class manages id values for new records. Here's a snippet from the orm.create() method:

    # Try-except added to filter the creation of those records whose filds are readonly.
    # Example : any dashboard which has all the fields readonly.(due to Views(database views))
    try:
        cr.execute("SELECT nextval('"+self._sequence+"')")
    except:
        raise except_orm(_('UserError'),
                    _('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
    id_new = cr.fetchone()[0]

It uses a sequence for each table to generate the new id value. The name of the sequence defaults to the name of the table plus '_id_seq', as you can see in the orm.__init__() method.

I don't know what you're trying to accomplish, but you might find it easier to use the orm class to create the record for you and let it take care of the details. For example, the create method returns the id value of the new record.

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