Search code examples
node.jssequelize.jscrud

Error connecting database with sequelize, can somebody help me?


an "s" is added to it and it doesn't connect me because it can't find a table with that name (clearly because of the added letter), I'm not finding the reason in the code, I'm stuck there.

Error: { "message": "Table 'entornoP.chevents' doesn't exist" } console:Executing (default): SELECT id, nombre, telefono, fecha, notas, createdAt, updatedAt FROM chevents AS chevent;

code:

import db from "../database/db.js";


import { DataTypes } from "sequelize";

    const BlogModel = db.define('chevent',{
        nombre: { type: DataTypes.STRING},
        telefono: { type: DataTypes.STRING},
        fecha: { type: DataTypes.STRING},
        notas: { type: DataTypes.STRING},
    })

export default BlogModel

Solution

  • By default Sequelize pluralizes table names from singular model names. You can change this behavior either on as concrete model level or globally in Sequelize options, see table name inference

    Example on the model level:

     const BlogModel = db.define('chevent',{
            nombre: { type: DataTypes.STRING},
            telefono: { type: DataTypes.STRING},
            fecha: { type: DataTypes.STRING},
            notas: { type: DataTypes.STRING},
        }, {
      freezeTableName: true   
    })