【问题标题】:Can Sequelize.js for Node.js define a table where the Primary Key is not named 'id'?Node.js 的 Sequelize.js 可以定义一个主键未命名为“id”的表吗?
【发布时间】:2014-08-16 18:04:38
【问题描述】:

我有一个名为 things 的简单 MySQL 表。我的things 表有三列:

Column Name  Data Type
===========  ============
thing_id     INT           // The auto increment Primary Key
thing_name   VARCHAR(255)
thing_size   INT

据我所知,Sequelize.js 期望/要求将主键命名为“id”。

有没有办法使用标准的sequelize.define() 调用来定义我的things 表?如果没有,还有其他方法吗?

【问题讨论】:

  • Sequelize 早期只支持“id”作为 PK,但在以后的版本中应该完全支持自定义主键。

标签: javascript mysql sql node.js sequelize.js


【解决方案1】:

试试

var Test = sequelize.define( 'things', {
    thing_id   : {type: Sequelize.INTEGER(11), primaryKey: true, autoIncrement: true},
    thing_name          : {type: Sequelize.STRING(255)},
    thing_size      : {type: Sequelize.INTEGER}
},{
});

Test.sync()
.success(function(){
    console.log('table created');
})
.error(function(error){
    console.log('failed', error)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多