节点 - 将表与 Sequelize 关联 [英] Node - associate tables with Sequelize

查看:12
本文介绍了节点 - 将表与 Sequelize 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以如下方式关联表格:

  • 一个组有很多文档
  • 一个文档属于一个组
  • 一个组属于一个用户
  • 一个用户有很多组

当我尝试列出表组"中的所有内容(包括用户")时,会抛出一个错误,指出用户未与组关联.

我的模特:

<块引用>

文档.js

const Document = conn.define('document', {标题: {类型:Sequelize.TEXT,允许空:假},内容: {类型:Sequelize.TEXT,允许空:假},页脚:{类型:Sequelize.TEXT,}})文档同步()Document.associate =(模型)=>{Document.belongsTo(models.Group, {foreignKey: 'groupId', as: 'Group'})}module.exports = 文档

<块引用>

Group.js

const Group = conn.define('group', {名称: {类型:Sequelize.STRING,允许空:假},})Group.sync()Group.associate =(模型)=>{Group.hasMany(models.Document, {foreignKey: 'groupId', as: 'Document'})Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'})}module.exports = 组

<块引用>

用户.js

const User = conn.define('user', {名: {类型:Sequelize.STRING,允许空:假},姓: {类型:Sequelize.STRING,允许空:假},电子邮件: {类型:Sequelize.STRING,},密码: {类型:Sequelize.STRING},})用户同步()User.associate =(模型)=>{User.hasMany(models.Group, {foreignKey: 'groupId', as: 'Group'})}module.exports = 用户

解决方案

我发现您的代码中有一个错误.您应该为一对多关系使用相同的外键名称.否则,您的数据库中将有两个不同的列.

这里你应该使用 userId 作为外键.Sequelize 在belongsTo"中创建 id 列.模型.所以如果你像这样使用它,Group模型中会有一个userId:

User.hasMany(models.Group, {foreignKey: 'userId', as: 'Group'});Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'});

也尽量不要在模型文件中创建关联.考虑使用 index.js 文件,您可以在其中创建所有关联.

src应用程序.js楷模index.js用户.jsgroup.js文档.js

将模型定义保存在他们的文件中.导出创建的模型类.将它们包含在您的 index.js 文件中.创建必要的关联并再次导出.

src/models/index.js

const User = require('./user');const Group = require('./group');const Document = require('./document');User.hasMany(models.Group, {foreignKey: 'userId', as: 'Group'});Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'});//定义你需要的关联.模块.exports = {用户,团体,文档};

I'm trying to associate tables in such a way that:

  • A group has many documents
  • A document belongs to a group
  • A group belongs to an user
  • An user has many groups

When i try to list all from table 'Group' including 'User', throws an error saying that User is not associated to Group.

My models:

Document.js

const Document = conn.define('document', {
    title: {
        type: Sequelize.TEXT,
        allowNull: false
    },
    content: {
        type: Sequelize.TEXT,
        allowNull: false
    },
    footer: {
        type: Sequelize.TEXT,
    }

})

Document.sync()
Document.associate = (models) => {
    Document.belongsTo(models.Group, {foreignKey: 'groupId', as: 'Group'})
}

module.exports = Document

Group.js

const Group = conn.define('group', {
    name: {
        type: Sequelize.STRING,
        allowNull: false
    },
})

Group.sync()
Group.associate = (models) => {
    Group.hasMany(models.Document, {foreignKey: 'groupId', as: 'Document'})
    Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'})
}

module.exports = Group

User.js

const User = conn.define('user', {
    firstName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    lastName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
    },
    password: {
        type: Sequelize.STRING
    },
})

User.sync()
User.associate = (models) => {
    User.hasMany(models.Group, {foreignKey: 'groupId', as: 'Group'})
}

module.exports = User

解决方案

I see one mistake in your code. You should use the same foreign key name for one to many relationships. Otherwise you will have two different columns in your database.

Here you should use userId as foreign key. Sequelize creates the id column in "belongsTo" model. So there will be a userId in Group model if you use it like this:

User.hasMany(models.Group, {foreignKey: 'userId', as: 'Group'});
Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'});

Also try not creating the associations in model files. Consider using index.js files and you can create all of your associations in there.

src
    app.js
    models
        index.js
        user.js
        group.js
        document.js

Keep you model definition in their files. Export the created model classes. Include them in your index.js file. Create necessary associations and export them again.

src/models/index.js

const User = require('./user');
const Group = require('./group');
const Document = require('./document');

User.hasMany(models.Group, {foreignKey: 'userId', as: 'Group'});
Group.belongsTo(models.User, {foreignKey: 'userId', as: 'User'});

// Define what associations you need.

module.exports = {
    User,
    Group,
    Document
};

这篇关于节点 - 将表与 Sequelize 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆