0
respostas

Problema no relacionamento entre tabelas

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class TitularUC extends Model {
    static associate(models) {
      this.belongsTo(models.Cliente)
      this.belongsTo(models.Endereco)
    }
  };
  TitularUC.init({
    nome: DataTypes.STRING,
    cpf: DataTypes.STRING,
    uc: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'TitularUC',
    tableName: 'TitularUC',
    freezeTableName: true,
    underscored:true,
    timestamps:false
  });
  return TitularUC;
};
'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Endereco extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      this.hasOne(models.TitularUC, {foreignKey: 'idTitular'})
    }
  };
  Endereco.init({
    end: DataTypes.STRING,
    cidade: DataTypes.STRING,
    bairro: DataTypes.STRING,
    cep: DataTypes.STRING(10),
    referencia: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'Endereco',
    tableName: 'Endereco',
    freezeTableName: true,
    underscored:true,
    timestamps:false
  });
  return Endereco;
};

com este código gera este erro: 'Cannot add or update a child row: a foreign key constraint fails (soluthec_development.titularuc, CONSTRAINT titularuc_ibfk_1 FOREIGN KEY (idEndereco) REFERENCES e ndereco (id))',