Paulo Henrique Peres
Paulo Henrique Peres

Reputation: 33

SequelizeDatabaseError: relation "equipment" does not exist

I´m using Sequelize and I have a follow code:

migration file:

'use strict'

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('equipments', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      hostname: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true
      },
      vendor: {
        type: Sequelize.STRING,
        allowNull: false
      },
      model: {
        type: Sequelize.STRING,
        allowNull: false
      },
      equipment_class_id: {
        type: Sequelize.INTEGER,
        allowNUll: false,
        references: { model: 'equipment_classes' }
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false
      }
    })
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('equipments')
  }
}

Model form Equipment:

const { Model, DataTypes } = require('sequelize')

class Equipment extends Model {
  static init (sequelize) {
    super.init({
      hostname: DataTypes.STRING,
      vendor: DataTypes.STRING,
      model: DataTypes.STRING,
      equipmentClassId: {
        type: DataTypes.INTEGER,
        field: 'equipment_class_id'
      }
    }, {
      sequelize
    })
  }
}

module.exports = Equipment

In the database, the table was created named equipments.

CREATE TABLE public.equipments
(
    id integer NOT NULL DEFAULT nextval('equipments_id_seq'::regclass),
    hostname character varying(255) COLLATE pg_catalog."default" NOT NULL,
    vendor character varying(255) COLLATE pg_catalog."default" NOT NULL,
    model character varying(255) COLLATE pg_catalog."default" NOT NULL,
    equipment_class_id integer,
    created_at timestamp with time zone NOT NULL,
    updated_at timestamp with time zone NOT NULL,
    CONSTRAINT equipments_pkey PRIMARY KEY (id),
    CONSTRAINT equipments_hostname_key UNIQUE (hostname)
,
    CONSTRAINT equipments_equipment_class_id_fkey FOREIGN KEY (equipment_class_id)
        REFERENCES public.equipment_classes (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

When I run this code:

async index (req, res) {
    const equipments = await Equipment.findAll()

    return res.json(equipments)
  }

I received error:

Executing (default): SELECT "id", "hostname", "vendor", "model", "equipment_class_id" AS "equipmentClassId", "created_at" AS "createdAt", "updated_at" AS "updatedAt" FROM "equipment" AS "Equipment";
(node:17032) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: relation "equipment" does not exist

My database: Postgresql 12

I have other tables that were created similarly and I don't have this problem, what am I doing wrong?

Paulo

Upvotes: 0

Views: 473

Answers (1)

David Kamer
David Kamer

Reputation: 2885

Your issue is naming. Sequelize uses an automatic rewrite of names for model to database table.

In your migration you have:

return queryInterface.createTable('equipments'...

But your model has:

class Equipment extends Model {

The naming convention pluralizes the name in the model to find in the database. So you should have, in your migration file:

return queryInterface.createTable('Equipment'...

Equipment is the plural form of singular equipment.

You could also make this much simpler by using sequelize-cli.

With sequelize-cli you don't have to worry about naming conventions. Just run (replacing the attribute list with your list of attributes):

sequelize model:generate --name Equipment --attributes fun:string,done:integer

And it should handle pluralization for you, generating a migration and model file.

Upvotes: 1

Related Questions