gpt4 book ai didi

javascript - Node JS API Sequelize PostgreSQL UUID 作为主键返回错误 "column Nan does not exist"

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

我正在使用 Node JS PostgreSQL Sequelize 为简单电子商务应用程序的后端开发 REST API,当我尝试将产品添加到购物车。它返回一个错误 "column Nan does not exist"最初我使用 Integer 作为用户 Id 作为主键,然后我更改了 UUID 以更好地适应目的。
我用于模型和迁移的代码如下:

//User model
export default (sequelize, DataTypes) => {
const User = sequelize.define(
'User',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: {
name: 'user_email',
msg: 'A user with this email already exists.'
}
},
},
User.associate = models => {
User.hasOne(models.Cart, {
foreignKey: 'userId',
as: 'cart',
onDelete: 'cascade'
});
};

User.associate = models => {
User.hasMany(models.Order, {
foreignKey: 'userId',
as: 'orders',
onDelete: 'cascade'
});
};

return User;
};

//User migration
export const up = (queryInterface, Sequelize) =>
queryInterface.createTable('Users', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
unique: true,
},
name: {
allowNull: false,
type: Sequelize.STRING
},
password: Sequelize.STRING,
email: {
allowNull: false,
type: Sequelize.STRING,
unique: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
});

export const down = queryInterface => queryInterface.dropTable('Users');
推车型号
export default (sequelize, DataTypes) => {
const Cart = sequelize.define('Cart', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
userId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false
},
cartItem: {
type: DataTypes.TEXT,
allowNull: false,
get(value) {
return JSON.parse(this.getDataValue(value));
},
set(value) {
this.setDataValue('cartItem', JSON.stringify(value));
}
}
});

Cart.associate = models => {
Cart.belongsTo(models.User, {
foreignKey: 'userId',
as: 'owner'
});
};
return Cart;
};
购物车迁移
export const up = (queryInterface, Sequelize) =>
queryInterface.createTable('Carts', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
userId: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
cartItem: {
type: Sequelize.TEXT,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
});

export const down = queryInterface => queryInterface.dropTable('Carts');
处理添加到购物车的代码:
addToCart() {
return this.asyncWrapper(async (req, res) => {
const { body, user } = req;

body.userId = user.id;
const cart = await this.service.addToCart(body);

this.sendResponse(res, cart, undefined, 201);
});
}
添加到购物车服务
async cart(userId, options = {}) {
const cart = await super.find({ userId }, options);
return cart;
}

async addToCart(data, options) {
const { userId, productId, qty } = data;

const [result] = await this.model.findOrCreate({
where: { userId: +userId },
defaults: { cartItem: new CartItem() }
});

const cartData = JSON.parse(result.dataValues.cartItem);
const cartItem = new CartItem(cartData);

const product = await ProductService.getById(productId, { plain: true });

ExceptionHandler.throwErrorIfNull(product);

const cart = cartItem.addToCart(product, qty);

result.cartItem = cart;
result.save();

return result;
}
Sequelize 生成的 SQL 查询如下: SELECT "id","userId","cartItem","createdAt","updatedAt" FROM "Carts" AS "Cart" WHERE "Cart"."userId" = NaN LIMIT 1;目标是使用 UUID 作为数据库中的主键。
当我将数据类型从 Integer 更改为 UUID 时,这个问题就开始了,我看不出代码有什么问题。
关于如何解决这个问题的任何建议?
Sequelize version: "^5.21.9" with "pg": "^8.2.0" and "pg-hstore": "^2.3.3".

最佳答案

如果您将数据类型从 INTEGER 切换为 UUID ,则不应尝试将 UUID-string 转换为执行 where: { userId: +userId } 的数字。
按原样传递 userId:

where: { userId }

关于javascript - Node JS API Sequelize PostgreSQL UUID 作为主键返回错误 "column Nan does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65462717/

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