了解续集中的联想

时间:2019-05-14 03:27:10

标签: mysql node.js join sequelize.js associations

我试图了解Sequelize中的关联。我从现有的数据库表开始,因此某些字段可能与Sequelize中的默认值不匹配。我使用Sequelizer直接从数据库生成模型。 我已经习惯于编写查询,但是现在我正在尝试学习像Sequelize这样的ORM的工作原理。

这是我的模特。

models / user.js

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    "User",
    {
      id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        field: "id"
      },
      username: {
        type: DataTypes.STRING(20),
        allowNull: false,
        field: "username"
      },
      fullname: {
        type: DataTypes.STRING(60),
        allowNull: false,
        field: "fullname"
      },
      createdat: {
        type: DataTypes.DATE,
        allowNull: false,
        field: "createdat"
      },
      updateat: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "updateat"
      },
      deletedat: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "deletedat"
      }
    },
    {
      tableName: "users",
      timestamps: false
    }
  );

  User.associate = function(models) {
        models.User.hasMany(models.Ticket),
        { as: "createdbyname", foreignKey: "createdby" };
  };
  return User;
};

models / ticket.js

module.exports = (sequelize, DataTypes) => {
  const Ticket = sequelize.define(
    "Ticket",
    {
      id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        field: "id"
      },
      details: {
        type: DataTypes.STRING(45),
        allowNull: true,
        field: "details"
      },
      assignedto: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        field: "assignedto"
      },
      createdby: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        field: "createdby"
      },
      createdat: {
        type: DataTypes.DATE,
        allowNull: false,
        field: "createdat"
      },
      updatedat: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "updatedat"
      },
      deletedat: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "deletedat"
      }
    },
    {
      tableName: "tickets",
      timestamps: false
    }
  );

  Ticket.associate = function(models) {
        models.Ticket.belongsTo(models.User,
            { foreignKey: "createdby" });
  };
  return Ticket;
};

在路由处理程序中,我按如下方式调用User.findAll:

  models.User.findAll({
    include: [models.Ticket]
  })

我希望看到的结果是一个查询,如下所示:

SELECT 
    `User`.`id`,
    `User`.`username`,
    `User`.`fullname`,
    `User`.`createdat`,
    `User`.`updateat`,
    `User`.`deletedat`,
    `Tickets`.`id` AS `Tickets.id`,
    `Tickets`.`details` AS `Tickets.details`,
    `Tickets`.`assignedto` AS `Tickets.assignedto`,
    `Tickets`.`createdby` AS `Tickets.createdby`,
    `Tickets`.`createdat` AS `Tickets.createdat`,
    `Tickets`.`updatedat` AS `Tickets.updatedat`,
    `Tickets`.`deletedat` AS `Tickets.deletedat`
FROM
    `users` AS `User`
LEFT OUTER JOIN
    `tickets` AS `Tickets` ON `User`.`id` = `Tickets`.`createdby`

我看到在控制台中运行的查询是:

SELECT 
    `User`.`id`,
    `User`.`username`,
    `User`.`fullname`,
    `User`.`createdat`,
    `User`.`updateat`,
    `User`.`deletedat`,
    `Tickets`.`id` AS `Tickets.id`,
    `Tickets`.`details` AS `Tickets.details`,
    `Tickets`.`assignedto` AS `Tickets.assignedto`,
    `Tickets`.`createdby` AS `Tickets.createdby`,
    `Tickets`.`createdat` AS `Tickets.createdat`,
    `Tickets`.`updatedat` AS `Tickets.updatedat`,
    `Tickets`.`deletedat` AS `Tickets.deletedat`,
    `Tickets`.`UserId` AS `Tickets.UserId`
FROM
    `users` AS `User`
LEFT OUTER JOIN
    `tickets` AS `Tickets` ON `User`.`id` = `Tickets`.`UserId`;

请注意LEFT OUTER JOIN子句的区别。这会引发如下错误:

Unhandled rejection SequelizeDatabaseError: Unknown column 'Tickets.UserId' in 'field list'

我需要一些帮助来弄清楚我哪里出了错。

2 个答案:

答案 0 :(得分:2)

在定义关联时,例如belongsTo,可以指定foreignKeytargetKeyforeignKey对应于源表中的字段(请记住,语法为sourceModel.belongsTo(targetModel, options))。 targetKey对应于目标表中的字段。

在您的情况下,您在 models / ticket.js 文件中的关联中犯了一个错误,您使用了:

models.Ticket.belongsTo(models.User, { foreignKey: "createdby" });

在这里,foreignKey引用了源表Ticket。因此,您正在告诉Sequelize将createdBy表的字段TicketUser表的默认键(主键)一起使用。由于createdBy中不存在Ticket,因此Sequelize会退回到默认情况下,即使用Ticket.UserID

要修复关联(和查询),您需要将belongsTo更新为以下内容:

models.Ticket.belongsTo(models.User, { targetKey: "createdby" });

答案 1 :(得分:0)

  Plant.findAll({
        include: Farm,
        order: [['name', 'ASC']]
        })
        .then(data=> {
            res.send(data)
        })
        .catch(err => {
            res.send(err)
            console.log(err);
            
        })
        
let arrQuery = [
      queryInterface.addColumn('farms', 'dayPlayed' , Sequelize.STRING),
      queryInterface.removeColumn('plants', 'dayPlayed' , Sequelize.STRING),
    ]
      return Promise.all(arrQuery)