gpt4 book ai didi

node.js - Sequelize 不生成连接表或获取/添加方法

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:59 25 4
gpt4 key购买 nike

我运行 Node 10.16.3 并 Sequelize 5.19.5。我使用 sequelize model:generate 创建了两个模型,它们之间具有多对多关系。它创建了模型和迁移文件。据说,当我指定多对多关联时,sequelize 应该自行生成联接表,并在这些模型上生成添加方法,以便我可以正确关联它们。在我运行 sequelize db:migratesequelize db:seed:all 后,这些事情都没有发生。我看到很多人只是手动创建连接表,但如果有更简单的方法,我想避免这种情况。代码文件如下(省略了一些不相关常量的导入):

模型事件

module.exports = (sequelize, DataTypes) => {
const Activity = sequelize.define('Activity', {
name: DataTypes.ENUM(cleaningActivity),
category: DataTypes.STRING,
baseRate: DataTypes.FLOAT,
specialEquipment: DataTypes.STRING,
description: DataTypes.STRING,
deleted: DataTypes.BOOLEAN
}, {});
Activity.associate = function(models) {
Activity.belongsToMany(models.ActivityBundle, {
through: 'Activity_ActivityBundle'
});
};
return Activity;
};

事件迁移:

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Activity', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.ENUM(cleaningActivity)
},
category: {
type: Sequelize.STRING
},
baseRate: {
type: Sequelize.FLOAT
},
specialEquipment: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
deleted: {
type: Sequelize.BOOLEAN
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Activity');
}
};

现在,ActivityBundle 模型:

module.exports = (sequelize, DataTypes) => {
const ActivityBundle = sequelize.define('ActivityBundle', {
name: DataTypes.ENUM(cleaningBundle),
deleted: DataTypes.BOOLEAN
}, {});
ActivityBundle.associate = function(models) {
ActivityBundle.belongsToMany(models.Activity, {
through: 'Activity_ActivityBundle'
});
};
return ActivityBundle;
};

最后,ActivityBundle 迁移:

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('ActivityBundle', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.ENUM(cleaningBundle)
},
deleted: {
type: Sequelize.BOOLEAN
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('ActivityBundle');
}
};

然后,我有一个种子文件,我尝试在其中首先创建 bundle ,然后将它们与事件相关联,如下所示:

module.exports = {
up: async (queryInterface, Sequelize) => {

const bundles = await ActivityBundle.bulkCreate(bundlesArray);

return Activity.bulkCreate(activitiesArray).then(activities => {
return Promise.all(activities.map(activity => {
const bundlesPerActivity = activityBundleMapping[activity.get('name')]
.map(name => bundles.find(b => b.get('name') === name));
return activity.addBundles(bundlesPerActivity); // this method does not exist, even though it should
}));
})
},
down: (queryInterface, Sequelize) => {
}
};

显然我做错了什么。我还应该定义什么以及在哪里?我想迁移文件需要提及多对多关联?没有任何线索,恕我直言,官方文档也不完整。

最佳答案

您需要自己生成连接表。 Sequelize 并没有那么神奇。 :)

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable(
'Activity_ActivityBundle',
{
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
ActivityId: {
type: Sequelize.INTEGER,
primaryKey: true,
},
ActivityBundleId: {
type: Sequelize.INTEGER,
primaryKey: true,
},
}
);
},

down: (queryInterface, Sequelize) => {
// remove table
return queryInterface.dropTable('Activity_ActivityBundle');
},
};

关于node.js - Sequelize 不生成连接表或获取/添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59954687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com