Reputation: 131
Good day!
When I am creating associations between models User and Test, sequelize works fine.
But when I want add new associations for Test and Question, then sequelize add new column (my foreign key test_id
and extra testId
).
Why?
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;
};
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;
};
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;
};
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;
These models generate some SQL queries:
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`
Upvotes: 0
Views: 223
Reputation: 131
I found a problem.
Test.associate = (models) => {
Test.hasMany(models.question, {
foreignKey: 'id', // was foreign_key: 'id'
});
};
May be is it a bug or trigger for autocreate reverse association? But i doesn't found information about that.
Upvotes: 0
Reputation: 12439
Add underscored: false
in all your models:
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
Upvotes: 1