【问题标题】:Sequelize Association Error Not AssociatedSequelize 关联错误未关联
【发布时间】:2016-04-11 08:28:48
【问题描述】:

我正在尝试将我的用户模型与我的组织模型相关联,但我遇到了一个错误,上面写着 Error: user is not associated to organization!,尽管我正在遵循将用户与我的组织相关联的过程。是否有可能是我使用的关联方法类型导致了问题?

用户模型(user.js):

var bcrypt   = require('bcrypt-nodejs');

module.exports = function(sequelize, DataTypes) {

var User = sequelize.define('user', {
    user_id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    firstName: {
        type: DataTypes.STRING,
        field: 'first_name'
    },
    lastName: {
        type: DataTypes.STRING,
        field: 'last_name'
    },
    email: {
        type: DataTypes.STRING,
        isEmail: true,
        unique: true
    },
    password: DataTypes.STRING,
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        allowNull: true
    }
}, {
    freezeTableName: true,
    classMethods: {
        generateHash: function(password) {
            return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
        },
        associate: function(db) {
            User.belongsTo(db.Organization, {foreignKey: 'organizationId'});
        },
    },
    instanceMethods: {
        validPassword: function(password) {
            return bcrypt.compareSync(password, this.password);
        },
    },


});
    return User;
}

组织模型(organization.js):

module.exports = function(sequelize, DataTypes) {

var Organization = sequelize.define('organization', {
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        autoIncrement: true,
        primaryKey: true
    },
    organizationName: {
        type: DataTypes.STRING,
        field: 'organization_name'
    },
    admin: DataTypes.STRING,
    members: DataTypes.STRING
},{
    freezeTableName: true
});

    return Organization;
}

要连接的表的索引 (db-index.js):

var Sequelize = require('sequelize');
var path = require('path');
var config = require(path.resolve(__dirname, '..', '..','./config/config.js'));
var sequelize = new Sequelize(config.database, config.username, config.password, {
    host:'localhost',
    port:'3306',
    dialect: 'mysql'
});

sequelize.authenticate().then(function(err) {
    if (!!err) {
        console.log('Unable to connect to the database:', err)
    } else {
        console.log('Connection has been established successfully.')
    }
});

var db = {}

db.Organization = sequelize.import(__dirname + "/organization");

db.User = sequelize.import(__dirname + "/user");

db.Records = sequelize.import(__dirname + "/records");

db.User.associate(db);
db.Records.associate(db);

db.sequelize = sequelize;
db.Sequelize = Sequelize;

sequelize.sync();

module.exports = db;

是触发此错误的路由调用:

appRoutes.route('/sign-up/organization')

    .get(function(req, res){
        models.User.find({
            where: {
                user_id: req.user.email
            }, attributes: [ 'user_id', 'email'
            ]
        }).then(function(user){
            res.render('pages/sign-up-organization.hbs',{
                user: req.user
            });
        })

    })

    .post(function(req, res, user){
        models.Organization.create({
            organizationName: req.body.organizationName,
            admin: req.body.admin,
            user: {
                organizationId: req.body.organizationId
            }
        }, { include: [models.User] }).then(function(){
            console.log(user.user_id);
            res.redirect('/app');
        }).catch(function(error){
            res.send(error);
            console.log('Error at Post');
        })
    });

【问题讨论】:

    标签: express sequelize.js


    【解决方案1】:

    需要设置反向关联,Organization hasMany Users

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2014-03-17
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多