-1

I wanna create a graphql server in NODEJS/KOA using graphql, sequelize with the Postgres database. I have a Postgres database with more than 19 tables and break all many to many relations to one-to-many.

for example here one of the table(Supplier) which has one-to-many relations with others.

ERD Diagram for Supplier table

I defined models like this for supplier and wants to define association for it too. how can do this?

module.exports = (sequelize, DataTypes) => {
  const Supplier = sequelize.define(
    'supplier',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      name: DataTypes.STRING,
      email: DataTypes.STRING,
      title: DataTypes.STRING,
      description: DataTypes.STRING,
      log_file_id: DataTypes.STRING,
      type: DataTypes.INTEGER,
      last_updated_on: DataTypes.TIMESTAMPTZ,
      last_updated_by_id: DataTypes.INTEGER,
      last_updated_by_text: DataTypes.STRING,
      created_on: DataTypes.TIMESTAMPTZ,
      created_by_id: DataTypes.INTEGER,
      created_by_text: DataTypes.STRING,
      password: DataTypes.STRING,
    },
    {
      freezeTableName: true,
    },
  )
  Supplier.associate = models => {
    Supplier.hasMany(models.files, models.contact,models.contact_detail)
  }

  return Supplier
}
Prix
  • 19,417
  • 15
  • 73
  • 132

1 Answers1

0
Supplier.associate = models => {
    Supplier.hasOne(models.files, {as: "Files", foreignKey: "supplier_id"});
    Supplier.hasOne(models.contact, {as: "Contacts", foreignKey: "supplier_id"});
    Supplier.hasOne(models.contact_detail, {as: "ContactDetails", foreignKey: "supplier_id"})
  }
Greg Belyea
  • 858
  • 1
  • 5
  • 15