gpt4 book ai didi

sequelize.js - Sequelize belongsToMany 关系不起作用

转载 作者:行者123 更新时间:2023-12-03 22:16:56 25 4
gpt4 key购买 nike

首先,我正在使用一个函数加载我的模型:

module.exports = function(config) {
var db, files, modelPath, myDb, sequelize;
sequelize = new Sequelize(config.database, config.username, config.password, {
dialect: 'postgres',
host: config.host,
port: config.port,
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci'
}
});
db = {};
modelPath = __dirname + "/models";
files = fs.readdirSync(modelPath);
_.each(files, function(file) {
var model;
if ('.coffee' === path.extname(file)) {
model = sequelize["import"](path.join(modelPath, file));
return db[model.name] = model;
}
});
Object.keys(db).forEach(function(modelName) {
if ('associate' in db[modelName]) {
return db[modelName].associate(db);
}
});
myDb = _.assign(db, {
sequelize: sequelize,
Sequelize: Sequelize
});
return myDb;
};

我有 2 个相关的模型:
module.exports = function(sequelize, DataTypes) {
var User;
User = sequelize.define('User', {
name: {
type: DataTypes.STRING,
allowNull: false
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataTypes.STRING,
allowNull: false
},
status: {
type: DataTypes.ENUM('active', 'inactive'),
defaultValue: 'active'
},
api_key: {
type: DataTypes.STRING,
allowNull: true
}
}, {
instanceMethods: {
display: function() {
var user;
user = {
name: this.name,
email: this.email,
username: this.username,
active: this.active
};
return user;
}
}
}, {
classMethods: {
associate: function(models) {
return User.belongsToMany(models.Entity, {
through: 'UserEntity'
});
}
}
});
User.hook('beforeCreate', function(user, options, beforeCreateCb) {
var uniqueKey;
uniqueKey = (JSON.stringify(user)) + " " + (new Date()) + " " + (Math.random());
user.api_key = crypto.createHash('md5').update(uniqueKey).digest('hex');
return beforeCreateCb(null, user);
});
return User;
};


module.exports = function(sequelize, DataTypes) {
var Entity;
return Entity = sequelize.define('Entity', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
classMethods: {
associate: function(models) {
return Entity.belongsToMany(models.User, {
through: 'UserEntity'
});
}
}
});
};

我通过以下方式找到了一个用户:
myDb.User.find({
where: {
username: 'snakamoto'
}
}).then(function(dbUser) {
var entities;
should.exist(dbUser.id);
entities = dbUser.getEntities();
return done();
});

但是,我收到一个错误: TypeError: Object [object SequelizeInstance] has no method 'getEntities'
不知道我做错了什么。如果重要的话,我正在使用 2.0.1

最佳答案

这可能是自动复数的问题。 sequelize 文档的以下部分可能包含解决此问题的提示:

http://docs.sequelizejs.com/en/latest/docs/associations/#naming-strategy

可能您需要在关联中告诉 sequelize 实体的复数是什么样的,在您的情况下是“实体”。

祝你好运。

关于sequelize.js - Sequelize belongsToMany 关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com