Scaffold generator for prisma relations

I’ve finished the Redwoodjs tutorial using Redwoodjs 0.2.5 version (os: Windows 10, node : 12.6.1, yarn: 1.21.1)

I generated a scaffold for product using this schema after saving and committing the db
‘yarn rw g scaffold product’

//schema.prisma
datasource DS {
provider = “sqlite”
url = env(“DATABASE_URL”)
}

generator photonjs {
provider = “prisma-client-js”
}

model Product {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime
variant Variant[]
}

model Variant {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime
title String
product Product
}

Using Form generated by the scaffold, how do I edit and save ‘variant’ in ‘Post’ Model and edit ‘product’ in ‘Variant’

  1. I am gettings errors at web and api side
    a) not defined fields for product in Variant and not defined fields for variant in ‘Product’ in generated query
    b) while submitting the data to the server, ID is a string when Int is expected errors from the remote graphql server

Ah, there’s a lot of conversation in process about getting relations to work properly. Could you see if this example helps answer your question?

And re: Form – you’ll need to build your own form for this as well (not using the generators).

Appreciate you extending the concepts and trying to make them work! Keep us posted.

This might be new with Redwood’s move to Prisma 2.0 but I got the existing form scaffold working. It just needs some additional logic in the service methods.

Consider this schema,

model RepoMeasurement {
  id           Int        @id @default(autoincrement())
  createdAt    DateTime   @default(now())
  repository   Repository @relation(fields: [repositoryId], references: [id])
  metric       Metric     @relation(fields: [metricId], references: [id])
  value        Int
  repositoryId Int
  metricId     Int
}

When I use the scaffold it creates the following, which has no consideration of the @relation,

export const createRepoMeasurement = ({ input }) => {
  return db.repoMeasurement.create({
    data: input,
  })
}

Because of that, Prisma complains about unknown fields repositoryId and metricId being passed in and necessary fields repository and metric being omitted. This simple change makes it work,

export const createRepoMeasurement = ({ input }) => {
  const { repositoryId, metricId, ...localFields } = input
  const data = {
    ...localFields,
    repository: { connect: { id: repositoryId } },
    metric: { connect: { id: metricId } },
  }
  return db.repoMeasurement.create({
    data,
  })
}

I assume this could be generated by the scaffolding system. I’d be willing to try with a head nod from maintainers. It wouldn’t handle the case in which the foreign row isn’t defined but that wouldn’t be much surprise because the generated form field takes an integer. It is surprising that even that connection doesn’t work.

1 Like

Nice work @turadg! And apologies for not making this forum article more findable/available/(insert another lame excuse here) :laughing:: Prisma Beta.2 and RWJS: Limited Generator Support for Relations (with workarounds)

I’d be willing to try with a head nod from maintainers.

^^ I want to fan this flame but need to provide a little background first! Back in April, we didn’t know yet where Prisma syntax and features for relations would settle. So we didn’t want to offer full generator support until that happened. (Or at least then have the discussion about the appropriate scope of generator support.) Originally the workaround from my forum topic above was going to live in api/lib/db.js, but this also felt like we’d potentially run into backward compatibility issues. (aside: then we talked about creating our own wrapper for the Prisma Client, which we would then import in db.js, which could possibly allow some magic but at least make it clear where the magic was coming from. So this is why we kept the db.js)

So where we are today is that relations are a confusing, sticky point for a lot of people. But there are a lot of types of relations that Prisma can handle (here’s the list).

To discuss:

  • Does Redwood Scaffold support generators and, if so, which types?
  • If not all types, how will Redwood handle the DX when a developer runs a Scaffold generators? (Currently it’s kind of a worst case false positive – generator ran successfully, CRUD is busted)

Given all that, what are your initial thoughts/reactions? I’m game to keep exploring, just say the word.

1 Like