I have these two tables created, one called “products”, and another called “cart”, I want to enter in the “cart” table the same products that I have in the “products” table in the form of an array of objects in which each object be a product, but I have the following error when trying to make a post (save) of the products:
code: ‘UNKNOWN_CODE_PLEASE_REPORT’,
errno: 4025,
sqlMessage: ‘CONSTRAINT cart.products failed for one_comfort.cart‘,
sqlState: ‘23000’,
index: 0,
sql: “insert into cart (idCart, products) values (18779, ‘[object Object]’)”
How can i solve this?
my “products” table is like this:
[
{
"id": 23448,
"price": 60,
"title": "ADIDAS TEE HULK",
"date": "6/6/2022, 3:34:43 AM",
"description": "ADIDAS TEE HULK UNISEX 100% COTTON.",
"stock": "3",
"code": "551A0",
"image": "https://sevensport.vteximg.com.br/arquivos/ids/388207-1000-1000/GP3398_1.jpg?v=637599860976670000",
"category": "t-shirts",
"new": true,
"logo": "https://www.chelsea.com.ar/arquivos/adidas-ch.jpg",
"genre": "men"
},
{
"id": 30463,
"price": 50,
"title": "PUMA EVIDE GRAPHIC TEE",
"date": "6/6/2022, 3:37:02 AM",
"description": "PUMA EVIDE GRAPHIC TEE WOMEN'S T-SHIRT",
"stock": "3",
"code": "111A0",
"image": "https://sevensport.vteximg.com.br/arquivos/ids/407751-500-500/59974701_1.jpg?v=637644782507230000",
"category": "t-shirts",
"new": true,
"logo": "https://www.chelsea.com.ar/arquivos/puma-ch.jpg",
"genre": "men"
}
]
and i want my “cart” table to be like this:
[
{
"id": 62621,
"date": "6/28/2022, 3:43:04 AM",
"products": [
{
"id": 57698,
"title": "Camisa no cool!",
"price": 230,
"image": "https://sevensport.vteximg.com.br/arquivos/ids/414267-1000-1000/02128G8_1.jpg?v=637659506619730000",
"description": "FULL DRICE!",
"stock": 3,
"date": "6/28/2022, 3:43:04 AM",
"code": "GAFG",
"category": "men"
},
{
"id": 57692,
"title": "Camisa cool!",
"price": 230,
"image": "https://sevensport.vteximg.com.br/arquivos/ids/414267-1000-1000/02128G8_1.jpg?v=637659506619730000",
"description": "FULL DRICE!",
"stock": 3,
"date": "6/28/2022, 3:43:04 AM",
"code": "GAFG",
"category": "men"
}
]
}
]
[mySqlPersistence]
const { knexConfig } = require("../../services/MYSQL/config");
const knex = require("knex")(knexConfig);
knex.schema
.createTableIfNotExists("products", (table) => {
table.increments("id").primary();
table.string("title");
table.string("description");
table.integer("price");
table.integer("stock");
table.string("image");
table.string("code");
table.string("category");
table.boolean("new");
table.timestamp("date").defaultTo(knex.fn.now());
table.string("logo");
table.string("genre");
})
.then(() => {
return knex.schema.createTableIfNotExists("cart", (t) => {
t.increments("idCart").primary();
t.timestamp("dateCart").defaultTo(knex.fn.now());
t.json("products");
t.integer("product_id").unsigned();
t.timestamp("date").defaultTo(knex.fn.now());
t.foreign("product_id").references("id").inTable("products");
});
})
.catch((err) => {
console.log("Error creating table:", err);
});
module.exports = knex;
[containerMySql]
const { knexConfig } = require("./config");
const knex = require("knex")(knexConfig);
const { customAlphabet } = require("nanoid");
const nanoid = customAlphabet("1234567890", 5);
class Container {
constructor(table) {
this.table = table;
}
async findAll() {
const element = await knex(this.table);
return element;
}
async save(data) {
return await knex(this.table).insert(data);
}
async find(id) {
const element = await knex(this.table).where("id", id);
return element;
}
async update(id, data) {
const element = await knex(this.table).where("id", id).update(data);
return element;
}
async getCarts() {
const element = await knex(this.table);
return element;
}
async deleteAll() {
await knex(this.table).delete();
}
async deleteOne(id) {
await knex(this.table).where("id", id).delete();
}
async getProducts(idCart) {
const element = await knex(this.table).where("id", idCart);
return element;
}
}
class KnexProductsContainer extends Container {
async find(id) {
const element = await knex(this.table).where("id", id).select("*");
return element;
}
async save(data) {
try {
await knex(this.table).insert(data);
} catch (error) {
console.log("FAIL " + error);
}
}
async update(id, data) {
const element = await knex(this.table).where("id", id).update(data);
return element;
}
}
class KnexCartContainer extends Container {
async find(id) {
const element = await knex(this.table).where("id", id).select("*");
return element;
}
async getProducts(idCart) {
const element = await knex(this.table).where("id", idCart).select("*");
return element;
}
async save(data) {
const newCart = {
idCart: Number(nanoid()),
products: [],
};
newCart.products.push({
product_id: Number(nanoid()),
title: data.title,
price: data.price,
logo: data.logo,
stock: data.stock,
description: data.description,
category: data.category,
genre: data.genre,
image: data.image,
code: data.code,
});
await knex(this.table).insert(newCart);
return newCart;
}
async update(id, data) {
const element = await knex(this.table).where("id", id).update(data);
return element;
}
}
module.exports = { Container, KnexProductsContainer, KnexCartContainer };