当我使用 Waterline 的创建方法时,它不会返回新记录的 ID

2022-01-15 00:00:00 sails.js mariadb mysql waterline

当我使用 Waterline 的 create 方法创建新记录时,返回创建的对象,但没有它的 ID.如何获取最后插入的对象的 id?

When i create a new record using Waterline's create method, the created object is returned, but without it's ID. How can I get the id of the last inserted object?

如果有什么不同,我会在 mariadb 上使用 mysql 适配器

I'm using the mysql adapter on mariadb if it makes any difference

推荐答案

看来你可能做错了什么.我没有对水线做任何额外的配置,反正每次我使用 new 查询和记录时它都会返回 id .

It seems like you're probably doing something wrong. I have not done any additional configuration to waterline, anyway it returns id every time when I use new query as well as record at all.

在大多数情况下,我不会在 Sails 模型中创建 id 字段,自动生成对我来说效果很好.如果您自己创建 id 字段,请确保它具有正确的配置.这个问题显示 id 需要是

In most cases I don't create id field in sails model, auto-generation works quite fine for me. If you create id field by yourself ensure that it has correct configuration. This issue shows that id requires to be

autoIncrement: true

否则它真的不会在查询中返回 id.

otherwise it really don't return id with queries.

完整字段应如下所示:

id: {
    type: 'integer', 
    primaryKey: true,
    autoIncrement: true
}

相关文章