简体   繁体   中英

Sequelize Query, many to many relationship issue

I currently have two MSSQL tables. An "Item" table, which contains an Item "Name" and a "Pallet" table which contains a Pallet "Barcode". I have a join table for these called "ItemPallets", that links the multiple Items to multiple Pallets.

"ItemPallets" contains the Item Id (Primary Key) and the Pallet Id (Primary Key) as well as a "Serial Number".

An Example of the data could be:

Item

+ -- + ---- +
| Id | Name |
+ -- + ---- +
| 1  | 123  |
+ -- + ---- +

Pallet

+ -- + ------- +
| Id | Barcode |
+ -- + ------- +
| 1  | ABC     |
| 2  | DEF     |
+ -- + ------- +

ItemPallets

+ -- + ------ + -------- + ------------- +
| Id | ItemId | PalletId | Serial Number |
+ -- + ------ + -------- + ------------- +
| 1  | 1      | 1        | a400          |
| 2  | 1      | 1        | a401          |
| 3  | 1      | 1        | a402          |
| 4  | 1      | 2        | a403          |
+ -- + ------ + -------- + ------------- +

This means that there are three items with the name 123 in Pallet ABC, with Serial Numbers a400, a401 and a402 respectively, and a final item with the name 123 in Pallet DEF with the serial number a403.

I am attempting to get back all of the ItemPallet rows that are associated with the Item Pallet, ie Item.Pallet[0].ItemPallets SHOULD contain an array, and I expected that array to contain three Rows, with serial numbers a400, a401 and a402.

However, while my array contains both Pallets, why do each of these pallets only contain a single serial number?

The code I currently am using is as follows:

models.Item.findOne({
  where: {id: 1},
  include: [
    { model: models.Pallet }
  ]
});

The query generated by your code returns three rows - you can see that by adding raw: true to the options next to where and include . I managed to conclude that Sequelize only takes into consideration the last returned row - if you would change the order of the query to opposite, you would obtain ItemPallets with serial number a403.

As for now I suggest you do it the other way

models.ItemPallets.findAll({
    where: {
        ItemId: 1
    }
}).then((itemPallets) => {
    // here you get all ItemPallets of Item with id 1
});

I will try to find out why Sequelize behaves like that in this situation and let you know if I know anything useful.

EDIT

According to your comment there is also a possibility of doing this by querying the Item table

models.Item.findByPrimary(1, {
    include: [ models.ItemPallets ]
}).then((itemWithItemPallets) => {
    // here you get (example output) { id: 1, ItemPallets: [{ id: 1, ItemId: 1, PalletId: 1, serialNumber: a400 }, { ... }]
});

Above query does

`JOIN ItemsPallets ON ItemsPallets.ItemId = Item.id WHERE Item.id = 1`

so it returns all ItemPallets of Item with id = 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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