Bug when testing the setup of Model and Migration connection

26 Views Asked by At

I'm currently doing a backend connection with a db, using node, postgresql and sequelize as orm. I've been getting the same error when I try to make a test using the route and creating a static user, since I haven't created the Controller yet, just to get an response.json. The error is:

Executing (default): INSERT INTO "Users" ("id","name","email","password_hash","admin","createdAt","updatedAt") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "id","name","email","password_hash","admin","createdAt","updatedAt";
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error

 name: 'SequelizeDatabaseError',
  parent: error: relation "Users" does not exist

 original: error: relation "Users" does not exist

I've already tried changing the Model name, the name used in the created migration, using freezeTableName and also defining a name for the tableName. I will send all steps i've done, please, I really need some help here, been stuck with this for 2 days already, I will explode my computer for real...

First, i've configured the settings of the sequelize to show the folders paths(.sequelizerc):

const { resolve } = require('path')


module.exports = {
    config: resolve( __dirname, 'src', 'config', 'database.js'),
    'models-path': resolve( __dirname, 'src', 'app', 'models' ),
    'migrations-path': resolve( __dirname, 'src', 'database', 'migrations')
}

and a few configurations on how the sequelize will read the User Model(database.js):

module.exports = {
  dialect: "postgres",
  host: "localhost",
  username: "postgres",
  password: "jhschier",
  database: "jojosburger",
  define: {
    timestamps: true,
    underscore: true,
    underscoreAll: true,
    freezeTimeTable: true,
  },
};

Afterwards I created the first migration using

yarn sequelize migration:create --name=create-users

And configured the UP and DOWN methods(20240109145449-create-users.js):

"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable("users", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
        primaryKey: true,
      },

      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },

      email: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
      },

      password_hash: {
        type: Sequelize.STRING,
        allowNull: false,
      },

      admin: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        allowNull: false,
      },

      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },

      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable("users");
  },
};

Then, migrated using the sequelize-cli, and checked if the migration was done on PostBird. It was.

yarn sequelize db:migrate

Postbird check

After the migration was done, I went for the first Model, the User (User.js).

import Sequelize, { Model } from "sequelize";
import { define } from "../../config/database";

class User extends Model {
  static init(sequelize) {
    super.init(
      {
        name: Sequelize.STRING,
        email: Sequelize.STRING,
        password_hash: Sequelize.STRING,
        admin: Sequelize.BOOLEAN,
      },
      {
        sequelize,
      },
      {
        freezeTableName: true,
      }
    );
  }
}

export default User;

And did its config at the migrations folder(index.js):

import Sequelize from "sequelize";

import User from "../app/models/User.js";
import configDatabase from "../config/database";

const models = [User];

class Database {
  constructor() {
    this.init();
  }

  init() {
    this.connection = new Sequelize(configDatabase);
    models.map((model) => model.init(this.connection));
  }
}

export default new Database();

At the end, I went to the routes.js to make the test:

import { Router } from "express";
import { v4 } from "uuid";

import User from "./app/models/User";

const routes = new Router();

routes.get("/", async (request, response) => {
  const users = await User.create({
    id: v4(),
    name: "JoJo",
    email: "[email protected]",
    password_hash: "123321",
    admin: true,
  });

  return response.json(users);
});

export default routes;

And when I try localhost:3000 I always get a connection error and that huge error in my terminal, can't even fetch the .json from it. As I said, tried changing the name from 'users' to 'user', to 'User', to 'Users', and still the same error. Tried defining freezeTimeTable at both I will be eternally thankful for some help!

0

There are 0 best solutions below