Relationship belongsToMany does not return duplicates

Asked

Viewed 43 times

0

I have the following models:

module.exports = (sequelize, DataTypes) => {
    const TestPlans = sequelize.define('TestPlans', {
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        }
    })

    TestPlans.associate = models => {
        TestPlans.belongsToMany(models.Tests, {
            through: 'TestPlanTests',
            foreignKey: 'testPlanId',
            as: 'tests',
        })
    }

    return TestPlans
}
module.exports = (sequelize, DataTypes) => {
    const Tests = sequelize.define('Tests', {
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        }
    })

    Tests.associate = models => {
        Tests.belongsToMany(models.TestPlans, {
            through: 'TestPlanTests',
            foreignKey: 'testId',
            as: 'testPlans',
        })
    }

    return Tests
}

And the relationship table:

'use strict'

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('TestPlanTests', {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            testId: {
                type: Sequelize.INTEGER,
                allowNull: false,
                references: {
                    model: 'Tests',
                    key: 'id'
                },
                onDelete: 'CASCADE'
            },
            testPlanId: {
                type: Sequelize.INTEGER,
                allowNull: false,
                references: {
                    model: 'TestPlans',
                    key: 'id'
                },
                onDelete: 'CASCADE'
            },
            createdAt: {
                allowNull: false,
                type: Sequelize.DATE
            },
            updatedAt: {
                type: Sequelize.DATE
            }
        })
    },

    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('TestPlanTests')
    }
}

And the following service:


const express = require('express')
const app = express()
const {Tests, TestPlans} = require('./models')

app.get('*', async (req, res) =>{

    const include =  [{
        model: Tests,
        attributes: ['name', 'id'],
        through: { attributes : [] },
        as: 'tests',
    }]

    const testPlans = await TestPlans.findAll({
        include : include,
    })

    res.status(200).json(testPlans)

})

module.exports = app

SQL returns the following data:

inserir a descrição da imagem aqui

But it is returned:

[
    {
        "id": 1,
        "name": "teste",
        "createdAt": "2019-10-01T17:19:30.000Z",
        "updatedAt": "2019-10-01T17:19:30.000Z",
        "tests": [
            {
                "name": "teste1",
                "id": 1
            },
            {
                "name": "teste2",
                "id": 2
            }
        ]
    },
    {
        "id": 2,
        "name": "teste2",
        "createdAt": "2019-10-01T17:19:30.000Z",
        "updatedAt": "2019-10-01T17:19:30.000Z",
        "tests": []
    }
]

And what I need is:

[
    {
        "id": 1,
        "name": "teste",
        "createdAt": "2019-10-01T17:19:30.000Z",
        "updatedAt": "2019-10-01T17:19:30.000Z",
        "tests": [
            {
                "name": "teste1",
                "id": 1
            },
            {
                "name": "teste1",
                "id": 1
            },
            {
                "name": "teste2",
                "id": 2
            },
            {
                "name": "teste1",
                "id": 1
            },
        ]
    },
    {
        "id": 2,
        "name": "teste2",
        "createdAt": "2019-10-01T17:19:30.000Z",
        "updatedAt": "2019-10-01T17:19:30.000Z",
        "tests": []
    }
]

Somehow the "duplicated" data is not returned.

1 answer

0

N:M relationships in Sequelize.js ignore duplicate lines even the query returns values correctly.

A possible solution is to replace the belongsToMany relationship for two hasMany with the Junction Table.

TestPlans.hasMany(models.TestPlanTests, {
        foreignKey : 'testPlanId',
        as: 'tests',
})

Tests.hasMany(models.TestPlanTests, {
        foreignKey : 'testId',
        as: 'tests',
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.