我如何获得续集模型的主键?

Ang*_*eno 6 sequelize.js

基本上,给定一个实例或模型,我想a)知道主键是否存在b)知道该字段的名称

Ang*_*eno 8

感谢Soheil Jadidian的评论; 以下代码段返回找到的主键数组.因此,它也适用于复合键.

Model.describe().then(function (schema) {
    return Object.keys(schema).filter(function(field){
        return schema[field].primaryKey;
    });
}).tap(console.log);
Run Code Online (Sandbox Code Playgroud)


Mik*_*mov 6

看一下Model.primaryKeyAttributes,它是主键属性的字符串名称数组:

console.dir(Model.primaryKeyAttributes);

[ 'id' ]
Run Code Online (Sandbox Code Playgroud)