未在MySQL、PostgreSQL和SQLite中创建的Sequelize关联
我使用sequalize和MYSQL在模型中定义关联。但是在迁移之后,外键不会像sequelize文档中所解释的那样添加到目标模型中 我还尝试在模型和迁移文件中手动定义外键,但仍然没有在表之间创建关联。当我在PhpMyAdmin的关系视图中查看表时,不会创建外键约束或关系 我已经用SQLite和PostgreSQL尝试过了,结果都是一样的。我不知道我做错了什么。这些是模型未在MySQL、PostgreSQL和SQLite中创建的Sequelize关联,mysql,postgresql,express,sequelize.js,associations,Mysql,Postgresql,Express,Sequelize.js,Associations,我使用sequalize和MYSQL在模型中定义关联。但是在迁移之后,外键不会像sequelize文档中所解释的那样添加到目标模型中 我还尝试在模型和迁移文件中手动定义外键,但仍然没有在表之间创建关联。当我在PhpMyAdmin的关系视图中查看表时,不会创建外键约束或关系 我已经用SQLite和PostgreSQL尝试过了,结果都是一样的。我不知道我做错了什么。这些是模型 AURHOR MODEL //One author hasMany books 'use strict'; module.
AURHOR MODEL
//One author hasMany books
'use strict';
module.exports = (sequelize, DataTypes) => {
const Author = sequelize.define('Author', {
Name: DataTypes.STRING
}, {});
Author.associate = function(models) {
// associations can be defined here
Author.hasMany(models.Book)
};
return Author;
};
我希望sequelize按照文档中的指定在books表中添加authorId,但这不会发生
BOOK MODEL
//Book belongs to Author
'use strict';
module.exports = (sequelize, DataTypes) => {
const Book = sequelize.define('Book', {
Title: DataTypes.STRING
}, {});
Book.associate = function(models) {
// associations can be defined here
Book.belongsTo(models.Author)
};
return Book;
};
迁移后,不会在这两个表之间创建关联。
我还尝试在模型关联中定义自定义外键,如下所示:
//Author model
Author.hasMany(models.Book,{foreignKey:'AuthorId'})
//Book model
Book.belongsTo(models.Author,{foreignKey:'AuthorId'})
'use strict';
module.exports = (sequelize, DataTypes) => {
const Book = sequelize.define('Book', {
Title: DataTypes.STRING,
AuthorId:DataTypes.INTEGER
}, {});
Book.associate = function(models) {
// associations can be defined here
Book.belongsTo(models.Author,{foreignKey:'AuthorId'})
};
return Book;
};
这仍然不能解决问题
我已经开始在模型中定义外键,然后在关联中引用它们,如下所示:
//Author model
Author.hasMany(models.Book,{foreignKey:'AuthorId'})
//Book model
Book.belongsTo(models.Author,{foreignKey:'AuthorId'})
'use strict';
module.exports = (sequelize, DataTypes) => {
const Book = sequelize.define('Book', {
Title: DataTypes.STRING,
AuthorId:DataTypes.INTEGER
}, {});
Book.associate = function(models) {
// associations can be defined here
Book.belongsTo(models.Author,{foreignKey:'AuthorId'})
};
return Book;
};
但仍然没有建立任何关联
我最终决定在迁移文件中添加引用,如下所示:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Books', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
Title: {
type: Sequelize.STRING
},
AuthorId:{
type: Sequelize.INTEGER,
references:{
model:'Author',
key:'id'
}
}
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Books');
}
};
但当我运行这种迁移设置时,我得到了以下错误:错误:无法创建表dbname
books
(错误号:150“外键约束为I
不正确形成的“)
当我切换到PostgreSQL时,也会出现类似的错误
这个问题耽搁了我很长时间。我可能做错了什么。我正在将sequelize版本4.31.2与sequelize CLI一起使用 我在迁移中错误地引用了模型。 走错了路
AuthorId:{
type: Sequelize.INTEGER,
references:{
model:'Author',
key:'id'
}
}
// Notes the model value is in lower case and plural just like the table name in the database
AuthorId:{
type: Sequelize.INTEGER,
references:{
**model:'authors',**
key:'id'
}
}
正确的方法
AuthorId:{
type: Sequelize.INTEGER,
references:{
model:'Author',
key:'id'
}
}
// Notes the model value is in lower case and plural just like the table name in the database
AuthorId:{
type: Sequelize.INTEGER,
references:{
**model:'authors',**
key:'id'
}
}
这解决了我的问题。现在正在定义关联