Node.js 如何将Sequelize Auto中的引用转换为Sequelize中的关联?
我使用生成我的数据库定义。在定义中,它使用references属性定义哪些列是外键。但是,Node.js 如何将Sequelize Auto中的引用转换为Sequelize中的关联?,node.js,sequelize.js,Node.js,Sequelize.js,我使用生成我的数据库定义。在定义中,它使用references属性定义哪些列是外键。但是, $ cat models/account.js /* jshint indent: 2 */ module.exports = function(sequelize, DataTypes) { return sequelize.define('account', { id: { type: DataTypes.BIGINT, allowNull: false,
$ cat models/account.js
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
return sequelize.define('account', {
id: {
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
parent_account_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: 'account',
key: 'id'
}
},
master_account_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: 'account',
key: 'id'
}
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: ''
},
enabled: {
type: DataTypes.INTEGER(1),
allowNull: false,
defaultValue: '1'
},
account_type_id: {
type: DataTypes.INTEGER(11),
allowNull: false
},
billing_account_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: 'account',
key: 'id'
}
},
contact_name: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: ''
},
contact_email: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: ''
},
data_feeds_path: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: ''
},
requests: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '0'
},
true_impressions: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '0'
},
clicks: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '0'
},
actions: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '0'
},
conversions: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '0'
},
status_id: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '1',
references: {
model: 'status',
key: 'id'
}
},
status_modified_date: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: '0001-01-01 00:00:00'
},
date_created: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: '0001-01-01 00:00:00'
},
date_created_user_id: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '1',
references: {
model: 'user',
key: 'id'
}
},
date_modified: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: '0001-01-01 00:00:00'
},
date_modified_user_id: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: '1',
references: {
model: 'user',
key: 'id'
}
},
date_deleted: {
type: DataTypes.DATE,
allowNull: true
},
date_deleted_user_id: {
type: DataTypes.BIGINT,
allowNull: true,
references: {
model: 'user',
key: 'id'
}
}
}, {
tableName: 'account'
});
};
如果我尝试在Sequelize查询中使用联接,我将收到:
Unhandled rejection Error: X is not associated to Y!
at Model.validateIncludedElement (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:558:11)
at /Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:440:29
at Array.map (native)
at Model.validateIncludedElements (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:436:37)
at Model.aggregate (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1558:30)
at Model.<anonymous> (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1624:17)
at runCallback (timers.js:666:20)
at tryOnImmediate (timers.js:639:5)
我尝试了几乎所有的组合,但是关联没有正确定义,比如说account.id列是account\u audit\u log表的外键。我尝试了几乎所有我能看到和看不到的变化。自我联想不应该被定义为
Person.hasOne(Person, {as: 'Father'})
// this will add the attribute FatherId to Person
虽然我没有使用sequelize auto,但您是否找到了此映射的解决方案?
Person.hasOne(Person, {as: 'Father'})
// this will add the attribute FatherId to Person