ENUM Sequelize Error

Asked

Viewed 53 times

1

I get the error RangeError: Maximum call stack size exceeded in my model statement. Follow code below:

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

class Usuario extends Model {
    static init(sequelize) {
        Usuario.init({
            nome: DataTypes.STRING,
            nascimento: DataTypes.DATE,
            genero: DataTypes.ENUM('masculino', 'feminino'),
            email: DataTypes.STRING,
            disponibilidade: DataTypes.ENUM('presencial', 'online'),
            biografia: DataTypes.STRING,
            foto_perfil: DataTypes.STRING
        }, {sequelize})
    } 
}

module.exports = Usuario;

The error message:

C:\Users\helde\Documents\TCC\node_modules\sequelize\lib\data-types.js:739
  constructor(...args) {
             ^

RangeError: Maximum call stack size exceeded
    at new ENUM (C:\Users\helde\Documents\TCC\node_modules\sequelize\lib\data-types.js:739:14)
    at Object.apply (C:\Users\helde\Documents\TCC\node_modules\sequelize\lib\utils\class-to-invokable.js:14:14)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:8:31)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)
    at Function.init (C:\Users\helde\Documents\TCC\src\models\Usuario.js:5:17)

2 answers

1

This mistake happens because you are calling .init() in class Usuario recursively on Usuario.init(...), then it enters and infinite loop and generates this error.

The mistake has nothing to do with ENUM, keep the type code as is.

The right thing would be for you to call super.init(), that is to start the Model and not the class itself Usuario.

Try this way:

class Usuario extends Model {
    static init(sequelize) {
        super.init({
            nome: DataTypes.STRING,
            nascimento: DataTypes.DATE,
            genero: DataTypes.ENUM('masculino', 'feminino'),
            email: DataTypes.STRING,
            disponibilidade: DataTypes.ENUM('presencial', 'online'),
            biografia: DataTypes.STRING,
            foto_perfil: DataTypes.STRING
        }, {sequelize})
    } 
}
  • when I leave so and using an index function to return all users of the database of the other bug (Node:10908) Unhandledpromiserejectionwarning: Sequelizedatabaseerror: Table 'dh_quero_materia.usuarios' doesn’t exist

  • Can you create another question and show the bug log? It will be easier to help. Now you have to see how the user table is being created.

0

    class Usuario extends Model {
    static init(sequelize) {
        super.init({
            nome: DataTypes.STRING,
            nascimento: DataTypes.DATE,
            genero: DataTypes.ENUM('masculino', 'feminino'),
            email: DataTypes.STRING,
            disponibilidade: DataTypes.ENUM('presencial', 'online'),
            biografia: DataTypes.STRING,
            foto_perfil: DataTypes.STRING,
            senha: DataTypes.STRING
        },  { sequelize ,
             tableName: 'usuario' },
        )
    }
}

error was solved by adding the tableName property: 'user'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.