繁体   English   中英

序列化:如何将一个表中的两个字段与另一个表相关联

[英]Sequelize: How associate two fields from a table another table

我的续篇有以下表格设置。

const Accounts = sequelize.define('Accounts', {
  name: DataTypes.STRING,
});

const Transfers = sequelize.define('Transfers', {
  value: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
  },
  accountIdFrom: DataTypes.INTEGER,
  accountIdTo: DataTypes.INTEGER,
});

Transfers.belongsTo(Accounts, { foreignKey: 'accountIdFrom' });
Transfers.belongsTo(Accounts, { foreignKey: 'accountIdTo' });

const data = await Transfers.findAll({
  include: [{ model: Accounts }]
});

返回:

{
  "id": 1,
  "value": "10.00",
  "accountIdFrom": 1,
  "accountIdTo": 2,
  "Account": {
    "id": 2,
    "name": "Banco Neon",
  }
}

我尝试以这种方式使用关联设置,但是续集始终仅与一个字段关联,并且我希望它对两个字段都显示。 accountIdFromacountIdTo

预期收益应该是这样的,但是,它不起作用:

{
  "id": 2,
  "value": "10.00",
  "accountIdFrom": 2,
  "accountIdTo": 1,
  "AccountFrom": {
    "id": 2,
    "name": "Bank Two",
  },
  "AccountTo": {
    "id": 1,
    "name": "Bank One",
  }
}

您必须使用as:而不是foreignKey:

Transfers.belongsTo(Accounts, { as: 'accountFrom', onDelete: 'cascade', onUpdate: 'no action' });
Transfers.belongsTo(Accounts, { as: 'accountTo', onDelete: 'cascade', onUpdate: 'no action' });

这会给你在你的Accounts模型中的列accountFromIdaccountToId 因此,当您需要包括模型时,您将需要这样做:

Transfers.find( {
  where: {},
  include: [{
    model: db.Accounts,
    as: 'accountFrom'
  },{
    model: db.Accounts,
    as: 'accountTo'
  }]
})

我在@Ellebkey的帮助下找到了解决方案。 按照下面的代码。

create-transfers.js

module.exports = {
  up: (queryInterface, Sequelize) => (
    queryInterface.createTable('Transfers', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      value: {
        type: Sequelize.DECIMAL(10, 2),
        defaultValue: 0,
      },
      accountFromId: {
        type: Sequelize.INTEGER,
      },
      accountToId: {
        type: Sequelize.INTEGER,
      },
    })
      .then(() => (
        queryInterface.addConstraint('Transfers', ['accountFromId'], {
          type: 'foreign key',
          name: 'fk_transfers_account_from',
          references: { // Required field
            table: 'Accounts',
            field: 'id',
          },
          onDelete: 'cascade',
          onUpdate: 'no action',
        })
      ))
      .then(() => (
        queryInterface.addConstraint('Transfers', ['accountToId'], {
          type: 'foreign key',
          name: 'fk_transfers_account_to',
          references: { // Required field
            table: 'Accounts',
            field: 'id',
          },
          onDelete: 'cascade',
          onUpdate: 'no action',
        })
      ))
  ),
  down: queryInterface => queryInterface.dropTable('Transfers'),
};

模型/transfers.js

module.exports = (sequelize, DataTypes) => {
    const Transfers = sequelize.define('Transfers', {
        value: {
            type: DataTypes.DECIMAL(10, 2),
            defaultValue: 0,
        },
        accountFromId: DataTypes.INTEGER,
        accountToId: DataTypes.INTEGER,
    }, {});
    Transfers.associate = ({ Accounts }) => {
        Transfers.belongsTo(Accounts, { as: 'AccountFrom' });
        Transfers.belongsTo(Accounts, { as: 'AccountTo' });
    };
    return Transfers;
};

控制器/transfers.js

const data = await Transfers.find({
    where: {},
    include: [{
        model: database.Accounts,
        as: 'AccountFrom',
    }, {
        model: database.Accounts,
        as: 'AccountTo',
    }],
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM