Saturday 20 May 2017

SequelizeJS HasOne association error

I am relatively new to NodeJS and SequelizeJS and am facing a hasOne issue with a query I am building and I'd like to know your thoughts about this issue to find out where I gone wrong and the correct way to implement this query.

Association Here

The models where generated using sequelize-auto (pg-hstore).

Bloco Model:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('bloco_condominio', {
    id_bloco: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    id_condominio: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'condominio',
        key: 'id_condominio'
      }
    },
    nm_bloco: {
      type: DataTypes.STRING,
      allowNull: true
    },
    ic_status: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: "A"
    }
  }, {
    tableName: 'bloco_condominio'
  });
};

Apartamento Model:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('apartamento', {
    id_apartamento: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    id_condominio: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'condominio',
        key: 'id_condominio'
      }
    },
    nu_apto: {
      type: DataTypes.STRING,
      allowNull: true
    },
    id_bloco: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'bloco_condominio',
        key: 'id_bloco'
      }
    },
    ic_status: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: "A"
    },
    dt_incl: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    },
    dt_ult_alt: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    }
  }, {
    tableName: 'apartamento'
  });
};

Apartamento Service:

"use strict";
var model = require('../models');
var Utils = require('../utils/utils');

var service = {};
var Apartamento = model.apartamento;
var Bloco = model.bloco_condominio;
var Morador = model.morador;
var Pessoa = model.pessoa;

//Incluir relação OneToMany
Apartamento.hasMany(Morador, { as: "Moradores", foreignKey: 'id_apartamento' });
Morador.belongsTo(Apartamento, { foreignKey: 'id_apartamento' });

Morador.hasMany(Pessoa, { as: "Pessoa", foreignKey: 'id_pessoa' });
Pessoa.belongsTo(Morador, { foreignKey: 'id_pessoa' });

Bloco.hasMany(Apartamento, { as: "Bloco", foreignKey: 'id_bloco' });
Apartamento.hasMany(Bloco, { foreignKey: 'id_bloco' }); 

service.getApartamentoById = function(idApartamento) {
    return Apartamento.findById(idApartamento, {
            include: [
                { model: Morador, as: 'Moradores', include: [
                    { model: Pessoa, as: 'Pessoa'}
                ]},
                { model: Bloco, as: 'Bloco' }
            ]
        })
        .then(function(data) {
            return data;
        })
        .catch(function(err) {
            throw 'Erro ao consultar apartamento por ID: ' + err.message + ' - Request: '+JSON.stringify(idApartamento);
        });
};

I can perfectly retrieve the other hasMany associations, but still hasn't found a way to do so in the reverse way.

Do you guys have any idea of how I should approach this issue in the correct manner?

Thanks in advance for your help!

Best regards, Enrico Bergamo



via Enrico Bergamo

No comments:

Post a Comment