KJReactor
08/25/2020, 10:35 PMdata
contains the input for the new product
I checked the table it indeed refers to the designated home. However, I can't get the products
column in Home to refer to the set of keys it has products
is an array of the same type of key.id. I tried changing the above to this but it doesn't work:
``
return await ctx.prisma.store.update({
where: { store_id },
data: {
products: {
create: { ...data }
}
}
});
``nikolasburk
KJReactor
08/26/2020, 10:25 AMKJReactor
08/26/2020, 10:26 AMmodel store {
store_id Int @default(autoincrement()) @id
name String @unique
url String? @unique
products product[]
}
model product {
product_id String @id @default(cuid())
name String @unique
description String
date DateTime @default(dbgenerated())
dateUpdated DateTime?
cost Float
weight Float
height Float
width Float
rating Float @default(0)
store Int
store_productTostore store @relation(fields: [store], references: [store_id])
feedback feedback[]
productCategory productCategory[]
}
nikolasburk
However, I can't get theA few notes/questions I have about this statement: • there is nocolumn in Home to refer to the set of keys it hasproducts
is an array of the same type of key.id.products
products
column in the database. products
is a relation field that only exists in the Prisma schema but not in the DB.
• what do you mean with "in Home" exactly?KJReactor
08/26/2020, 10:32 AMKJReactor
08/26/2020, 10:33 AMKJReactor
08/26/2020, 10:33 AMKJReactor
08/26/2020, 10:33 AMnikolasburk
KJReactor
08/26/2020, 10:36 AMproduct_id
to the products
field for the designated storenikolasburk
const newProduct = await prisma.product.create({
data: {
...data,
store_productTostore: {
connect: {
store_id: 42
}
}
}
})
nikolasburk
nikolasburk
products
array on the respective store.KJReactor
08/26/2020, 10:44 AMKJReactor
08/26/2020, 10:44 AMstore
model / table is not updatedKJReactor
08/26/2020, 10:45 AMreturn ctx.prisma.store.update({
where: { store_id },
data: {
products: {
create: { ...data },
},
},
});
nikolasburk
store
table can't be updated because the table itself doesn't have a reference to the the product
table.eKJReactor
08/26/2020, 10:46 AMproducts
argument being unknownKJReactor
08/26/2020, 10:46 AMKJReactor
08/26/2020, 10:47 AMproducts
column in store to a foreign key but I'm confused how since its an array. I tried doing it like any other foreign key. It didn't worknikolasburk
const newStore = await prisma.store.create({
data: {
name: "Bathing store",
},
});
const data = {
name: "BubbleBath",
description: "asd",
cost: 0,
weight: 0,
height: 0,
width: 0,
};
const newProduct = await prisma.product.create({
data: {
...data,
store_productTostore: {
connect: {
store_id: newStore.store_id,
},
},
},
});
const storeWithProducts = await prisma.store.findMany({
include: {
products: true
}
})
console.dir(storeWithProducts, { depth: null})
nikolasburk
KJReactor
08/26/2020, 10:55 AMKJReactor
08/26/2020, 10:56 AMKJReactor
08/26/2020, 10:58 AMKJReactor
08/26/2020, 10:58 AMKJReactor
08/26/2020, 10:59 AMposts
field for the user
modelKJReactor
08/26/2020, 11:09 AMposts
field does not "manifest" in the underlying database schema*".nikolasburk
in the model example immediately under "one-to-many relations"Ah thanks for pointing this out! This is actually an error in the docs, you can't define the models like this:
model User {
id Int @id @default(autoincrement())
email String @unique
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
published Boolean @default(false)
author User?
}
This is missing the relation scalar field, here's what it should look like:
model User {
id Int @id @default(autoincrement())
email String @unique
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
KJReactor
08/26/2020, 11:10 AMKJReactor
08/26/2020, 12:21 PMproducts
on the Store
is as follows, correct?
return await ctx.prisma.store.findOne({
where,
include: {
product: true,
},
});
nikolasburk
product*s*: true
and not product: true
, but otherwise this looks right to me 👍nikolasburk
KJReactor
08/26/2020, 12:38 PMKJReactor
08/26/2020, 12:38 PMKJReactor
08/26/2020, 12:41 PMKJReactor
08/26/2020, 12:47 PMmodel product {
product_id String @id @default(cuid())
name String @unique
description String
date DateTime @default(dbgenerated())
dateUpdated DateTime?
cost Float
weight Float
height Float
width Float
rating Float @default(0)
store_id Int
store_productTostore store @relation(fields: [store_id], references: [store_id])
feedback feedback[]
productCategory productCategory[]
}
model store {
store_id Int @default(autoincrement()) @id
name String @unique
url String? @unique
products product[]
}
KJReactor
08/26/2020, 12:47 PMreturn await ctx.prisma.store.findOne({
where,
include: {
products: true,
},
});
nikolasburk
KJReactor
08/26/2020, 12:50 PMnikolasburk
prisma migrate save
and prisma migrate up
) or introspection?KJReactor
08/26/2020, 12:51 PMKJReactor
08/26/2020, 12:52 PMKJReactor
08/26/2020, 12:53 PMnikolasburk
store
and product
tables as well? Also make sure to re-run prisma generate
after any changes to the DB and Prisma schema.KJReactor
08/26/2020, 12:56 PMcreate table "product"(
"product_id" varchar(30) not null primary key,
"name" varchar(25) not null unique,
"description" text not null,
"date" date not null default current_date,
"dateUpdated" date,
"cost" float not null,
"weight" float not null,
"height" float not null,
"width" float not null,
"rating" float not null default 0,
"store_id" int not null,
foreign key ("store_id") references "store"("store_id")
);
create table "store"(
"store_id" serial primary key,
"name" varchar(25) not null unique,
"url" varchar(30) unique
);
KJReactor
08/26/2020, 12:56 PMnikolasburk
String
ID for the product
table but autoincrementing integers for store
? Any reason why product
shouldn't have the same ID type?KJReactor
08/26/2020, 1:47 PMnikolasburk
@default(cuid())
manually to the schema, that makes sense 👍KJReactor
08/26/2020, 1:49 PMnikolasburk
const store = await prisma.store.create({
data: {
name: 'MyShop3'
}
})
const product = await prisma.product.create({
data: {
cost: 0,
description: "A cool product",
name: "myProduct3",
weight: 0,
height: 0,
width: 0,
store: {
connect: {
store_id: store.store_id
}
}
}
})
const findStore = await prisma.store.findOne({
where: { store_id: store.store_id },
include: {
products: true
},
});
console.dir(findStore, { depth: null})
It logs:
{
store_id: 4,
name: 'MyShop3',
url: null,
products: [
{
product_id: 'ckebfvot60000xecbi27o3p8t',
name: 'myProduct3',
description: 'A cool product',
date: 2020-08-26T00:00:00.000Z,
dateUpdated: null,
cost: 0,
weight: 0,
height: 0,
width: 0,
rating: 0,
store_id: 4
}
]
}
nikolasburk
KJReactor
08/26/2020, 2:01 PMKJReactor
08/26/2020, 2:02 PMnikolasburk
KJReactor
08/26/2020, 2:07 PMnikolasburk
model product {
product_id String @id @default(cuid())
name String @unique
description String
date DateTime @default(dbgenerated())
dateUpdated DateTime?
cost Float
weight Float
height Float
width Float
rating Float @default(0)
store_id Int
store store @relation(fields: [store_id], references: [store_id])
}
model store {
store_id Int @default(autoincrement()) @id
name String @unique
url String? @unique
products product[]
}
KJReactor
08/26/2020, 2:11 PM