Add FK to table (association) in the model, sequlize in node (angular fullstack)

0

I need to add FK in the tables, I've tried it in the following way and I can not do it.

Index.html

db.Country = db.sequelize.import('../api/country/country.model');
db.City = db.sequelize.import('../api/city/city.model');
module.exports = db;

city.model.js

export default function (sequelize, DataTypes) {
  var City = sequelize.define('City', {
    _id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name: DataTypes.STRING,
    info: DataTypes.STRING,
    active: DataTypes.BOOLEAN
  }, {
      associate: function (db) {
        City.belongsTo(db.Country);
      }
    });
  return City;
}

country.model.js

export default function (sequelize, DataTypes) {
  var Country = sequelize.define('Country', {
    _id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name: DataTypes.STRING,
    info: DataTypes.STRING
   }, {
      associate: function (db) {
        Country.hasMany(db.City);
      }
    });
  return Country;
}

PS: I'm using postgres

    
asked by Alexis Granja 09.05.2017 в 21:08
source

1 answer

0

Solve by adding the following to the model.

 countryId: {
      type: DataTypes.INTEGER,
      references: {
        model: 'Countries',
        key: '_id'
      }
    }
    
answered by 10.05.2017 / 21:30
source