I got a One-to-Many relationship between two objects : Dashboard, and Chart, described like this :
module.exports = function (sequelize, DataTypes) {
const Dashboard = sequelize.define('Dashboard', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING
}
});
Dashboard.associate = (models) => {
Dashboard.belongsTo(models.User, {
foreignKey: 'user_id',
targetKey: 'id'
});
Dashboard.hasMany(models.Chart, {
foreignKey: 'dashboard_id',
sourceKey: 'id'
});
};
return Dashboard;
};
And :
module.exports = function(sequelize, DataTypes) {
var Chart = sequelize.define('Chart', {
id: {
type: DataTypes.INTEGER(32),
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
x_title: {
type: DataTypes.STRING
},
y_title: {
type: DataTypes.STRING
},
data_type: {
type: DataTypes.STRING,
allowNull: false
},
data_value: {
type: DataTypes.STRING,
allowNull: false
},
filters: {
type: DataTypes.TEXT,
allowNull: false
}
}
);
Chart.associate = (models) => {
Chart.belongsTo(models.Dashboard, {
foreignKey: 'dashboard_id',
targetKey: 'id'
});
};
return Chart;
};
So when I want to add a new Dashboard with several charts like this :
models.Dashboard.create({
user_id: 1,
title: 'Dashboard title',
charts: [
{
title: 'time',
x_title: 'Days',
y_title: 'Count',
data_type: 'count',
data_value: 'visit',
filters: '[{"type":"project","values":["1"]},{"type":"language","values":["french"]},{"type":"satisfaction","values":[1,2,3]}]'
},
{
title: 'indicator',
x_title: '',
y_title: '',
data_type: 'count',
data_value: 'visit',
filters: '[{"type":"project","values":["1","2","3","4","5","6"]},{"type":"language","values":["french"]}]'
}
]
}, { include: [models.Chart] }).then(() => {
res.send({
'message': 'Dashboard loaded !'
});
});
The dashboard is inserted into the database, but no charts inserted as well... What is the best way to add records with a one-to-many association using Sequelize ? I just try and try, and read all docs (http://docs.sequelizejs.com/manual/tutorial/associations.html#creating-with-associations) but I don't understand this problematic behaviour...
Thank's for your help and your enlightening !
答案 0 :(得分:1)
在定义的关联中
Dashboard.hasMany(models.Chart, {
foreignKey: 'dashboard_id',
sourceKey: 'id'
});
foreignKey
被称为dashboard_id
,但dashboard_id
模型中没有Chart
。
以下代码可以使用
const Dashboard = sequelize.define('dashboard', {
title: Sequelize.STRING,
});
const Chart = sequelize.define('chart', {
dashboard_id: Sequelize.INTEGER,
title: Sequelize.STRING,
});
Dashboard.hasMany(Chart, {
foreignKey: 'dashboard_id',
sourceKey: 'id'
});
Dashboard.create({
title: 'one',
charts: [
{ title: 'title1' },
{ title: 'title2' }
]
}, { include: [Chart] }).then((result) => {
console.log(result);
});
答案 1 :(得分:1)
感谢您的帮助,但我需要双向的一对多关联。
按照你的方式,我仍然遇到同样的问题......我不明白为什么,也许是因为我使用Sequelize CLI,每个对象定义使用一个JS文件......或者Sequelize在适当的时候不调用同步功能。
奇怪的是,当我首先插入仪表板然后通过定义它们的dashboard_id插入图表时,它运行良好。像这样:
models.Dashboard.create({
user_id: 1,
title: 'Visites Histopad'
}).then((dashboard) => {
models.Chart.bulkCreate([{
dashboard_id: dashboard.id,
title: 'time',
x_title: 'Days',
y_title: 'Count',
data_type: 'count',
data_value: 'visit',
filters: '[{"type":"project","values":["1"]},{"type":"language","values":["french"]},{"type":"satisfaction","values":[1,2,3]}]'
},
{
dashboard_id: dashboard.id,
title: 'indicator',
x_title: '',
y_title: '',
data_type: 'count',
data_value: 'visit',
filters: '[{"type":"project","values":["1","2","3","4","5","6"]},{"type":"language","values":["french"]}]'
}
]).then(() => {
res.send({
'message': 'Dashboard loaded !'
});
});
});
答案 2 :(得分:1)
好吧,我想通了......我使用的是旧版Sequelize,因为seuquelize-cli加载了2.0.0-rc8版本而不是最后一个版本(4.0.0)。所以现在一切都很完美。