0
votes

I'm working with sequelize associations. I've seen many ways to add foreign keys, is the way I'm doing it correctly?

What I'm trying to do is add a foreign key constraint to my profile_personals table from my parent table (users, "user_id")

const Sequelize = require('sequelize')
const db = require('../database/db.js')
const Users = require('../model/User.js')

module.exports = db.sequelize.define(
    'profile_personals',
    {   
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },

        biography: {
            type: Sequelize.STRING
        },
        fk_user_id: {
            type: Sequelize.INTEGER,

            references: {
                // This is a reference to another model
                model: Users,
                // This is the column name of the referenced model
                key: 'user_id'
            }
        }
    },
    {
        timestamps: false
    }
)
1

1 Answers

0
votes

Did you try association method? I think HasMany or HasOne methods are gonna work.

http://docs.sequelizejs.com/class/lib/associations/has-many.js~HasMany.html

user.hasMany(db.profile_personals, {foreignKey: 'user_id'})