【问题标题】:Sequelize findOrCreate insert new row but return object with primary key undefined续集 findOrCreate 插入新行但返回未定义主键的对象
【发布时间】:2014-01-29 13:53:33
【问题描述】:

我在一个小型 nodejs 项目中使用Sequelize,使用 mysql 数据库。

使用 findOrCreate,我可以找到现有对象并插入新对象,但是当创建并返回新对象时,主键是“未定义”。

定义

    module.exports = function(sequelize, DataTypes) {
        var Student = sequelize.define('Student', {
        id : {
            type : DataTypes.BIGINT,
            primaryKey : true
        },
        wxid : DataTypes.STRING,
        client : DataTypes.STRING,
        nick : DataTypes.STRING,

        wallet : {type: DataTypes.INTEGER, defaultValue: 0},
        mcount : {type: DataTypes.INTEGER, defaultValue: 0},
        qcount : {type: DataTypes.INTEGER, defaultValue: 0},

        status : {type: DataTypes.INTEGER, defaultValue: 0},
        premium_type : {type: DataTypes.INTEGER, defaultValue: 0},
        createtime : DataTypes.DATE,
        lastqtime : DataTypes.DATE,
        lastmtime : DataTypes.DATE

        }, {
        freezeTableName : true,
        timestamps : false,
        tableName : 'student_student',
        associate: function(models) {
            Student.hasMany(models.Question, {as:'questions', foreignKey: 'student_id'});
        }
        });

        return Student;
    };

findOrCreate

    db.Student.findOrCreate( { wxid : info.uid },
            {
                client: client_name,
                createtime: new Date(),
                lastmtime: new Date()
            }).success(
            function(stu, created) {
                if (!stu) {
                console.log('Stu[' + info.uid + '] not found.'.red);
                } else {
                if (created) {
                    stu.created = true;
                    console.log(stu.values);    
                    //// Here, stu.id is 'undefined'

                }
                }
            });

日志:

    [app-0 (out) 2014-01-10T21:54:32] { id: undefined,
    [app-0 (out) 2014-01-10T21:54:32]   wxid: 'o6ssct7SHGXN1zlyD6vvfk3TMr68',
    [app-0 (out) 2014-01-10T21:54:32]   client: 'wxcs',
    [app-0 (out) 2014-01-10T21:54:32]   nick: undefined,
    [app-0 (out) 2014-01-10T21:54:32]   wallet: 0,
    [app-0 (out) 2014-01-10T21:54:32]   mcount: 0,
    [app-0 (out) 2014-01-10T21:54:32]   qcount: 0,
    [app-0 (out) 2014-01-10T21:54:32]   status: 0,
    [app-0 (out) 2014-01-10T21:54:32]   premium_type: 0,
    [app-0 (out) 2014-01-10T21:54:32]   createtime: Fri Jan 10 2014 21:54:32 GMT+0800 (CST),
    [app-0 (out) 2014-01-10T21:54:32]   lastqtime: undefined,
    [app-0 (out) 2014-01-10T21:54:32]   lastmtime: Fri Jan 10 2014 21:54:32 GMT+0800 (CST) }

但是当我查看数据库时,我发现该行已正确插入。

如何找到或创建 id 集?谢谢。

【问题讨论】:

    标签: sql node.js sequelize.js


    【解决方案1】:

    将autoIncrement: true添加到id字段,解决问题。

    【讨论】:

    • 这也为我解决了这个问题,我的模型让 Sequelize 推断默认标识符行为(我的代码没有指定 id:{...},所以我必须添加整个 { type : DataTypes.BIGINT, primaryKey : true, autoIncrement: true })。
    【解决方案2】:

    我知道我来晚了,但你也可以声明一个默认的 beforeCreate 函数来帮助你解决这个问题。

    例如我使用 shortid 作为主键生成器,所以我的 beforeCreate hook 看起来像这样:

    define: {
      hooks: {
        beforeCreate: function(o) {
          if (!o.id) o.id = shortid.generate();
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 1970-01-01
      • 2021-07-10
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多