使用sequelize mysql在nodejs中保存数据时如何排除id

ubbxdtey  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(516)

我试图保存用户信息。但我的错误正在减少。
未处理的拒绝sequelizedatabaseerror:“field list”中的未知列“id”我的代码是

var userObj = {userid: "aa", "emailid": "aa@aa.com", "user_type": "ENG"};
User.user
    .build(userObj)
    .save()
    .then(anotherTask => {
        console.log(anotherTask);
    })
    .catch(error => {
        throw error;
    });

我的表中没有id列。如何排除id。

gj3fmq9x

gj3fmq9x1#

在您的问题中,您提到了如何排除id。
也可以删除 id 使另一个场 primaryKey: true ,

sequelize.define('user', {
userid: {
    type: Sequelize.STRING,
    allowNull: false,
    primaryKey: true
},
emailid: Sequelize.STRING,
user_type: Sequelize.STRING
},{freezeTableName: true, tableName: 'user', id: false});

所以,我补充道 primaryKey: trueuserid 现在检查一下 id 会从table上消失。

5gfr0r5j

5gfr0r5j2#

如果表中没有id,则将唯一列作为id字段。

sequelize.define('user', {
'id': {
    type: Sequelize.STRING,
    field: 'userid',
    allowNull: false,
    primaryKey: true
},
userid: Sequelize.STRING,
emailid: Sequelize.STRING,
user_type: Sequelize.STRING

},{freezetablename:true,tablename:'user',id:false});

{id: "aa", "userid": "aa", "emailid": "aa@aa.com", "user_type": "ENG"};

相关问题