Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot associate 1:N #5832

Closed
CanKer opened this issue May 2, 2016 · 1 comment
Closed

Cannot associate 1:N #5832

CanKer opened this issue May 2, 2016 · 1 comment

Comments

@CanKer
Copy link

CanKer commented May 2, 2016

What you are doing?

_Hello, Im trying to associate 2 tables but Im getting an error that says "hospitales" is not associated with "medicos"!

I have 2 tables "medicos" and "hospitales" where "medicos" belongsTo "hospitales" and "hospitales" has many "medicos"

Here you will find my code for hospitales table

module.exports = function(sequelize, DataTypes) {
  var hospitales = sequelize.define('hospitales', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    nombre: {
      type: DataTypes.STRING,
      allowNull: false
    },
    idDatos: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      references: {
        model: 'datos',
        key: 'id'
      }
    },
    piso: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    tableName: 'hospitales',
    freezeTableName: true,
    classMethods: {
      associate: models =>  {
        models.hospitales.hasMany(models.medicos,  {
          foreignKey: "id"
        })
      }
    }

  });
  return hospitales
};

here you will find the code for "medicos" table

/* jshint indent: 2 */

module.exports = function(sequelize, DataTypes) {
  var medicos = sequelize.define('medicos', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    nombre: {
      type: DataTypes.STRING,
      allowNull: false
    },
    apellidoP: {
      type: DataTypes.STRING,
      allowNull: false
    },
    apellidoM: {
      type: DataTypes.STRING,
      allowNull: false
    },
    nomina: {
      type: DataTypes.STRING,
      allowNull: false
    },
    idDatos: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      references: {
        model: 'datos',
        key: 'id'
      }
    },
    especialidad: {
      type: DataTypes.ENUM('Medico','Geriatra'),
      allowNull: false
    },
    HospitalesId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      references: {
        model: 'hospitales',
        key: 'id'
      }
    }
  }, {
    tableName: 'medicos',
    freezeTableName: true,
    classMethods: {
      associate: models =>  {
        models.medicos.belongsTo(models.hospitales,  {
          foreignKey: "id", as: "Hospitales"
        })
      }
    }
  });
  return medicos
};

and here you will find my controller where is my query

'use strict'

module.exports = (Medicos, Hospitales) =>  {

  const express   = require('express'),
        router    = express.Router()
  router
  .get('/', (req, res) => {
    Medicos.findAll({ include: [{ model: Hospitales, include: [Medicos], }], }).then((Medicos) => res.json(Medicos));
  })

  module.exports = router

}

What do you expect to happen?

I want to retrieve all the "medico" data like this

[
  {
    "id": 1,
    "nombre": "Name",
    "apellidoP": "lastName",
    "apellidoM": "mothersName",
    "nomina":     "SSN",
    "especialidad": "specialty",
    "hospital": {
                    "nombre": "Hospital name"
  },

What is actually happening?

Unhandled rejection Error: hospitales (hospitales) is not associated to medicos!

Dialect: mysql

@janmeier
Copy link
Member

janmeier commented May 2, 2016

models.medicos.belongsTo(models.hospitales,  {
          foreignKey: "id", as: "Hospitales"
        })

The as part has to be included in the find call as well.

Or you can save the output from the association, e.g. medicos.Hospitales = models.medicos.belongsTo... and then include {assocation: medicos.Hospitales }

See #4618, #3273 et. al.

@janmeier janmeier closed this as completed May 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants