sql - Sequelize findOrCreate 插入新行但返回主键 undefined object

标签 sql node.js sequelize.js

我使用 Sequelize在一个小型 nodejs 项目中,使用 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;
    };

查找或创建

    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 的 OrCreate?谢谢。

最佳答案

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

关于sql - Sequelize findOrCreate 插入新行但返回主键 undefined object ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21046903/

相关文章:

node.js - 在 azure blob 容器之间复制文件

javascript - Webpack 解析导入路径的目录(goCardless 作为样板)

node.js - 分享 Sequelize 模型的最佳方式

node.js - 带有 Feathers.js 和 Sequelize 的只读模式

sql - 直接从用户定义的函数中选择 xml 节点

java - 未找到 JDBC Derby 驱动程序

MYSQL 左连接相关表

sql - Oracle SQL 哪个更高效?并行创建表作为选择或并行插入?

json - 如何将 JSON 数组从 NodeJS 流式传输到 postgres

javascript - 需要 Sequelize 模型类时,module.exports undefined,什么是正确的结构?