gpt4 book ai didi

many-to-many - Sequelize不创建多对多连接表

转载 作者:行者123 更新时间:2023-12-02 08:20:55 26 4
gpt4 key购买 nike

当我启动服务器时,在建立数据库连接后不久,我会这样做:

var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()

tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});

tool.sync();
requirement.sync();

我希望看到正在创建的连接表,但它不存在。我错过了什么?

Executing (default): CREATE TABLE IF NOT EXISTS `requirements` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): CREATE TABLE IF NOT EXISTS `tools` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `idCode` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`requirements`)
Executing (default): PRAGMA INDEX_LIST(`tools`)

它说 in the documentation该表是为我创建的。我不应该自己手动创建一个。

我正在使用:

最佳答案

似乎我不应该在每个模型上调用 .sync(),我应该只调用一次 sequelize.sync()

因此:

var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()

tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});

sequelize.sync();

//DO NOT tool.sync();
//DO NOT requirement.sync();

这是我在我的项目中如何使用它的示例:link

关于many-to-many - Sequelize不创建多对多连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37396592/

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