gpt4 book ai didi

mysql - sequelize 给出了错误的表名

转载 作者:行者123 更新时间:2023-11-29 04:07:17 24 4
gpt4 key购买 nike

我有一个数据库。我有一个“user”表

我正在尝试使用 sequelize 创建我的第一个 REST 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);
}
}
});

最佳答案

要解决您的问题,您需要在选项对象中设置 freezeTableName = true。

例如

    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);
}
}
});

关于mysql - sequelize 给出了错误的表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546381/

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