我有一个数据库。我有一个“user
”表
我正在尝试使用REST
sequelize
api
然而,当它执行我的查询时,我在控制台中得到以下内容:
SELECT `id`, `username`, `password`, `name`, `organization_id`, `type_id`, `join_date` FROM `users` AS `user` WHERE `user`.`id` = '1';
你可以看到它试图使用一个名为users
的表但是这个表不存在。
以下是我的一些代码:
请告诉我你是否需要更多我不确定哪里出错了? :S
var User = sequelize.define('user', {
id: DataTypes.INTEGER,
username: DataTypes.STRING,
password: DataTypes.STRING,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
type_id: DataTypes.INTEGER,
join_date: DataTypes.STRING
}, {
instanceMethods: {
retrieveAll: function(onSuccess, onError) {
User.findAll({}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function(user_id, onSuccess, onError) {
User.find({where: {id: user_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function(onSuccess, onError) {
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.build({ username: username, password: password })
.save().ok(onSuccess).error(onError);
},
updateById: function(user_id, onSuccess, onError) {
var id = user_id;
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.update({ username: username,password: password},{where: {id: id} })
.success(onSuccess).error(onError);
},
removeById: function(user_id, onSuccess, onError) {
User.destroy({where: {id: user_id}}).success(onSuccess).error(onError);
}
}
});
答案 0 :(得分:6)
要解决您的问题,您需要在选项对象中设置freezeTableName = true。
e.g。
var User = sequelize.define('user', {
id: DataTypes.INTEGER,
username: DataTypes.STRING,
password: DataTypes.STRING,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
type_id: DataTypes.INTEGER,
join_date: DataTypes.STRING
}, {
freezeTableName: true,
instanceMethods: {
retrieveAll: function(onSuccess, onError) {
User.findAll({}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function(user_id, onSuccess, onError) {
User.find({where: {id: user_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function(onSuccess, onError) {
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.build({ username: username, password: password })
.save().ok(onSuccess).error(onError);
},
updateById: function(user_id, onSuccess, onError) {
var id = user_id;
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.update({ username: username,password: password},{where: {id: id} })
.success(onSuccess).error(onError);
},
removeById: function(user_id, onSuccess, onError) {
User.destroy({where: {id: user_id}}).success(onSuccess).error(onError);
}
}
});
答案 1 :(得分:0)
我之前没有使用过sequelize,但是从他们的文档中读取 - 可能是你遗失了 -
User.sync({force: true}).then(function () {
// Table created
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});
即。表创建部分......
谢谢