Sequelize association hasOne(one to one relationship) is creating 2 foreign keys in mySql (in expressJs)

22 views Asked by At

I am defining 2 columns us,books with primary key usId and bookId respectively. After that i am using association us.hasOne(books,{foreignKey:'usId'});books.belongsTo(us). These lines are now creating two forign keys with name usId and uUsId in books column . Where is this uUsId coming from?My sequelize version is 6.37 and mySql2 is of 3.9.1 version

import sequelize from "../../database/connect.js";
import { DataTypes } from "sequelize";


const us=sequelize.define('us',{
    usId:{
        type:DataTypes.INTEGER,
        allowNull:false,
        autoIncrement:true,
        primaryKey:true
    },
    name:{
        type:DataTypes.STRING,
        allowNull:false
    }
})

const books=sequelize.define('books',{
    bookId:{
        type:DataTypes.INTEGER,
        allowNull:false,
        autoIncrement:true,
        primaryKey:true
    },
    name:{
        type:DataTypes.STRING,
        allowNull:false
    }
})

us.hasOne(books,  {  foreignKey:'usId'})
books.belongsTo(us)

await us.sync({alter:true})
await books.sync({alter:true})
export default us

This above code is genrating two foreign keys(2 columns) with names uUsId and usId.This uUsId is comming no matter what I write in foreign key declaration line .What should i do ?

0

There are 0 answers