DaPoox
DaPoox

Reputation: 149

Sequelize association include returns null

I am having an issue when I'm trying to associate a table into my query with sequelize-cli.

My query works but it doesn't populate Adresse table. Only Patient is populated. Adresse array is ignored. (return null)

I made a one-to-one relationship between the tables and am not sure if that's the cause of the error or if it is somewhere else where I am associating the two tables.

here is my models :

server/models/patient.js

module.exports = (sequelize, Sequelize) => {
    const Patient = sequelize.define('Patient', {
        ///
    }, {
        classMethods: {
            associate: (models) => {
                Patient.belongsTo(models.Adresse, {
                    foreignKey: 'adresseId',
                });
            }
        }
    });
    return Patient;
};

server/models/adresse.js

module.exports = function(sequelize, Sequelize) {
    const Adresse = sequelize.define('Adresse', {
        adresse: {
            type: Sequelize.STRING,
            allowNull: false,
        },
        complementAdr: {
            type: Sequelize.STRING
        },
        codePostal: {
            type: Sequelize.INTEGER,
            allowNull: false
        },
    }, {
        classMethods: {
            associate: (models) => {
                Adresse.hasMany(models.Patient, {
                    foreignKey: 'adresseId',
                    as: 'Patients',
                });
            }
        }
    });
    return Adresse;
};

and here is where I specified the association on my migration files :

server/migrations/20170326145609-create-patient.js

adresseId: {
    type: Sequelize.INTEGER,
    references: {
        model: 'Adresses',
        key: 'id_adresse',
        as: 'adresseId',
    },
},

server/migrations/20170326145502-create-adresse.js

module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('Adresses', {
            id_adresse: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            adresse: {
                type: Sequelize.STRING,
                allowNull: false,
            },
            complementAdr: {
                type: Sequelize.STRING
            },
            codePostal: {
                type: Sequelize.INTEGER,
                allowNull: false
            },
            createdAt: {
                allowNull: false,
                type: Sequelize.DATE
            },
            updatedAt: {
                allowNull: false,
                type: Sequelize.DATE
            }
        });
    },
    down: function(queryInterface, Sequelize) {
        return queryInterface.dropTable('Adresses');
    }
};

and finally here is my query on my controller file :

server/controllers/patients.js

const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');
const Patient = require('../models').Patient;
const Adresse = require('../models').Adresse;

module.exports = {
    create(req, res) {
        return Patient
            .create({
                ///
                adressesId: {
                    adresse: req.body.adresse,
                    codePostal: req.body.codePostal,
                }
            }, {
                include: [{
                    model : Adresse
                }]
            })
            .then(patient => res.status(201).send(patient))
            .catch(error => res.status(400).send(error));
    }
};

Upvotes: 1

Views: 4268

Answers (1)

piotrbienias
piotrbienias

Reputation: 7411

Try using Adresse instead adresseId when eager creating the Adresse model instance related to given Patient

return Patient.create({
    // patient attributes,
    Adresse: {
        adresse: req.body.adresse,
        codePostal: req.body.codePostal
    },
    include: [ Adresse ]
}).then(patient => {
    // look at the query generated by this function
    // it should create both patient and adresse
});

Upvotes: 1

Related Questions