Sequelize associate

I try to do sequelize associate, I have user and note models. I want that user_id will be a foreing key for notes. And when user add some notes, the field with notes will have a user_id. What I do wrong?

User model

    const Sequelize = require('sequelize');
    const db = require('../config/database');
    const bcryptjs = require('bcryptjs');

    const Note = require('./note');

    const User = db.define('user', {
        userNick: {
            type: Sequelize.STRING,
            allowNull: false,
            validate: {
                notEmpty: {
                    args: true,
                    msg: 'This field must be filled'
                },
                len: [8, 50],
            },
            unique: true
        },
        userName: {
            type: Sequelize.STRING,
            allowNull: false,
            validate: {
                notEmpty: true,
                len: [3, 50]
            }
        },
        userPassword: {
            type: Sequelize.STRING,
            allowNull: false,
            validate: {
                notEmpty: true,
                len: [10, 128]
            }
        },
        userEmail: {
            type: Sequelize.STRING,
            allowNull: false,
            validate: {
                notEmpty: true,
            },
            unique: true
        },      
    });

    User.beforeSave((User) => {
        if (User.changed('userPassword')) {
            User.userPassword = bcryptjs.hashSync(User.userPassword, bcryptjs.genSaltSync(10), null);
        }
    })

    User.associate = function (Note) {
        User.hasMany(Note, { foreignKey: 'user_id' });
    };
    /*User.prototype.comparePassword = function (userPassword, cb) {
        bcryptjs.compare(userPassword, this.userPassword, function (err, isMatch) {
            if (err){
                return cb (err);
            }
            cb(null, isMatch);
        })
    }*/


    User.sync({ force: false });

    console.log("The table for the User model was just (re)created!");


    module.exports = User;


Note 

const Sequelize = require('sequelize');
const db = require('../config/database');

const User = require('../models/user');

const Note = db.define('note', {
    title: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
            notEmpty: {
                args: true,
                msg: 'This field must be filled'
            },
            len: [1, 50],
        },
    },
    description: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
            notEmpty: {
                args: true,
                msg: 'This field must be filled'
            },
            len: [1, 255],
        },
    },
    userId: {
        type: Sequelize.INTEGER,
        references: {
            model: 'users',
            key: 'id'
        }
    },
});

Note.associate = function (User) {
    Note.belongsTo(User, { targetKey: "user_id", constraints: false })
}

Note.sync({ force: true });
console.log("The table for the Note model was just (re)created!");

module.exports = Note;

Ответы (0 шт):