Sequelize foreign key error

Asked

Viewed 368 times

-1

Well, I was trying to implement the relationships with sequelize, I don’t know if I misunderstood the documentation but from what I understood it should work, below the model I want to make the relationships:

./model/Sindico.js

const db = require('../config/db');
const bcrypt = require('bcryptjs');
const Condominio = require('./Condominio')

const Sindico = db.sequelize.define('sindicos', {
    nome: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    senha: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    cpf: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    endereco: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    condominioId: {
        type: db.Sequelize.STRING,
        allowNull: false,
        references: {
            model: Condominio,
            key: 'nome'
        }
    }
})

Sindico.hasMany(Condominio)
Condominio.hasOne(Sindico)

Sindico.addHook('beforeCreate', (sindico, options) => {
    return bcrypt.hash(sindico.senha, 8).then(hash => {
        sindico.senha = hash;
    }).catch(err => {           //antes de cadastrar o sindico essa função criptografa a senha digitada
        throw new Error();
    })
})

Sindico.sync({force: true})
module.exports = Sindico

settings for database connection:

const Sequelize = require('sequelize');
const sequelize = new Sequelize('projetoteste', 'root', '', {
    host: 'localhost',
    dialect: 'mysql'        //aqui está configurado parâmetros para se conectar ao banco de dados,
})                          //atualmente está local mas logo mudará.

module.exports = {
    Sequelize: Sequelize,
    sequelize: sequelize
}

model of the Condominium: ./model/Condominio.js

const db = require('../config/db');
const Sindico = require('./Sindico');

const Condominio = db.sequelize.define('condominios', {
    nome: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    cnpj: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    endereco: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    responsavel: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    cpfResp: {
        type: db.Sequelize.STRING,
        allowNull: false
    },
    telefone: {
        type: db.Sequelize.STRING,
        allowNull: false
    }
})

Condominio.sync({force: true})

module.exports = Condominio

the following error points on the screen:

Unhandled rejection SequelizeDatabaseError: Can't create table `projetoteste`.`sindicos` (errno: 150 "Foreign key constraint is incorrectly formed")

1 answer

0


In the condominium model you need to add one of the columns as Unic

const Condominio = db.sequelize.define('condominios', {
    nome: {
        type: db.Sequelize.STRING,
        allowNull: false,
        unique: true
    },
    ...
  • thanks friend I’ll test here and I’ll tell you soon!

  • Thank you very much friend worked here!

Browser other questions tagged

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