gpt4 book ai didi

javascript - 为什么 Sequelize 5要这么做?

转载 作者:行者123 更新时间:2023-12-02 23:48:54 25 4
gpt4 key购买 nike

美好的一天!当我在模型 User 和 Test 之间创建关联时,sequelize 工作正常。但是,当我想为测试和问题添加新关联时,请 Sequelize 添加新列(我的外键 test_id 和额外的 testId )。

为什么?

用户.js

module.exports = (sequelize, DataType) => {
const User = sequelize.define('user', {
username: {
type: DataType.STRING,
unique: true,
},
password: {
type: DataType.STRING,
},
});

User.associate = (models) => {
User.hasMany(models.test, {
foreignKey: 'id',
});
};

return User;
};

测试.js

module.exports = (sequelize, DataType) => {
const Test = sequelize.define('test', {
title: {
type: DataType.STRING,
unique: true,
required: true,
},

description: {
type: DataType.TEXT,
required: true,
},

picture: { // picture link
type: DataType.STRING,
},
});

Test.associate = (models) => {
Test.belongsTo(models.user, {
foreignKey: 'user_id',
});
};

Test.associate = (models) => {
Test.hasMany(models.question, {
foreign_key: 'id',
});
};

return Test;
};

问题.js

module.exports = (sequelize, DataType) => {
const Question = sequelize.define('question', {
title: {
type: DataType.STRING,
required: true,
},
});

Question.associate = (models) => {
Question.belongsTo(models.test, {
foreignKey: 'test_id',
});
};
return Question;
};

index.js

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
require('dotenv').config();

const config = process.env;
const basename = path.basename(module.filename);

const sequelize = new Sequelize({
database: config.DB_NAME,
username: config.DB_USER,
password: config.DB_PASS,
dialect: config.DB_DIALECT,
});
const db = {};

fs
.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0) && (file !== basename))
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

Object.keys(db).forEach((modelName) => {
if ('loadScopes' in db[modelName]) {
db[modelName].loadScopes(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

这些模型生成一些 SQL 查询:

Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `username` VARCHAR(255) UNIQUE, `password` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;

Executing (default): SHOW INDEX FROM `users` FROM `nrforms`

Executing (default): CREATE TABLE IF NOT EXISTS `tests` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255) UNIQUE, `description` TEXT, `picture` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;

Executing (default): SHOW INDEX FROM `tests` FROM `nrforms`

Executing (default): CREATE TABLE IF NOT EXISTS `questions` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `test_id` INTEGER, `testId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`test_id`) REFERENCES `tests` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`testId`) REFERENCES `tests` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;

Executing (default): SHOW INDEX FROM `questions` FROM `nrforms`

最佳答案

在所有模型中添加underscored: false:

module.exports = (sequelize, DataType) => {
const Question = sequelize.define('question', {
title: {
type: DataType.STRING,
required: true,
},
},
{
underscored: false, // <---- add this in all your models
});

Question.associate = (models) => {
Question.belongsTo(models.test, {
foreignKey: 'test_id',
});
};
return Question;
};

Sequelize Documentation reference

关于javascript - 为什么 Sequelize 5要这么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55725632/

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