【问题标题】:Node JS Sequelize join queryNode JS Sequelize 连接查询
【发布时间】:2020-06-04 02:59:54
【问题描述】:

我是续集的新手,就像我今天必须学习它一样。我什至不知道如何对此提出问题,所以请让我知道如何改进。我有以下查询。

router.get('/:id/work', async (req, res) => {
    let results = await User.findAll(
        {
            where: {
                id: req.params.id
            },
            include: [Task, Project]
        }
    );
    res.status(200);
    res.send(results);
});

我想获取用户信息,并将“tasks”和“projects”作为“assigner”和“project_id”属性的数组。我的 Project 和 Task 模型都将这些列作为外键。 我得到的结果是下面那个


{
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
Task: {
  id: 9,
  name: "req.body.name",
  description: "req.body.description",
  score: 1,
  status: "req.body.status",
  assigner: 5,
  project_id: null
},
Project: {
  id: 2,
  name: "req.body.name",
  body: "req.body.body",
  status: null,
  assigner: 5
 }
},
 {
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
 Task: {
  id: 9,
  name: "req.body.name",
  description: "req.body.description",
  score: 1,
  status: "req.body.status",
  assigner: 5,
  project_id: null
 },
Project: {
  id: 3,
  name: "req.body.name",
  body: "req.body.body",
  status: "req.body.status",
  assigner: 5
 }
},

有什么快速的方法可以实现吗?

{
  id: 5,
  email: "asd@asdads.com",
  name: "asdasdasd",
  surname: "asdasdasdasd1231",
  Task: [/*all task objects*/],
  Project: [/*all project objects*/]
}

模型定义

const User = db.define('User', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    email: Sequelize.STRING,
    name: Sequelize.STRING,
    surname: Sequelize.STRING,
}, {
    timestamps: false,
    tableName: 'users'
})

/**
 * Model Relations
 */

Project.belongsTo(User, { foreignKey: 'assigner' })
User.hasOne(Project, { foreignKey: 'assigner' })

Task.belongsTo(User, { foreignKey: 'assigner' })
User.hasOne(Task, { foreignKey: 'assigner' })

Task.belongsTo(Project, { foreignKey: 'project_id' })

export default User;

const Task = db.define('Task', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: Sequelize.STRING,
    description: Sequelize.TEXT,
    score: Sequelize.INTEGER,
    status: Sequelize.STRING,
    assigner: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: 'id'
        }
    },
    project_id: {
        type: Sequelize.INTEGER,
        references: {
            model: Project,
            key: 'id'
        }
    }
}, {
    timestamps: false,
    tableName: 'tasks'
})


export default Task;
const Project = db.define('Project', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: Sequelize.STRING,
    body: Sequelize.STRING,
    status: Sequelize.STRING,
    assigner: {
        type: Sequelize.INTEGER,
        references: {
            model: User,
            key: 'id'
        }
    }
}, {
    timestamps: false,
    tableName: 'projects'
})

export default Project;

【问题讨论】:

  • 能否描述一下表关系以及上面的例子是如何存储的。还有你是如何配置用户、任务和项目模型之间的映射的。
  • @GhulamMohammedTaher 我已经与 Project.belongsTo(User) 建立了关系,如果我使用 hasMany(...) 进行操作,则会引发用户表没有的错误“分配者”列
  • 显示模型定义和关联
  • @Anatoly 已更新,谢谢!

标签: javascript node.js express sequelize.js


【解决方案1】:

将关联定义从 hasOne 更改为 hasMany,因为我看到许多项目可以链接到一个用户以及任务:

User.hasMany(Project, { foreignKey: 'assigner' })
User.hasMany(Task, { foreignKey: 'assigner' })

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 2022-01-19
    • 1970-01-01
    • 2017-07-29
    • 2014-08-24
    • 2015-10-12
    • 2015-05-30
    • 2016-11-25
    相关资源
    最近更新 更多