How to create Prisma model with related item

SeanMarc

Here I have two models, a venue and an address. Would it be better practise to have the venue address within the venue model or is it best to keep them separate? The second question would be how do I create a venue with a linked address? Having read the Prisma documentation I can not seem to find the answer I am looking for. I am getting errors from the pine that icludes propertyNumber like so

Type '{ title: string; images: string[]; address: { propertyNumber: string; }; }' is not assignable to type '(Without<VenueCreateWithoutUserInput, VenueUncheckedCreateWithoutUserInput>

  await prisma.user.create({
    data: {
      name: 'Samss',
      email: '[email protected]',
      Venue: {
        create: { 
          title: 'new venue',
          images: ['https://cdn.pixabay.com/photo/2023/01/31/05/59/zebra-7757193_960_720.jpg'],
          address: {
            propertyNumber: "12"
          },
          
        }
      },
    },
    })

model Venue {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String   @db.VarChar(255)
  address   Address? @relation(fields: [addressId], references: [id])
  published Boolean  @default(false)
  userId    String
  images    String[]
  user      User     @relation(fields: [userId], references: [id])
  addressId String?
}

model Address {
  id             String  @id @default(cuid())
  propertyNumber String
  firstLine      String
  Area           String
  Country        String
  zipCode        String
  lat            Decimal
  lng            Decimal
  Venue          Venue[]
}
Jordan Quagliatini

I'd say that it will depend on how often you will need the venues without the address? If it's never, just put the address fields inside the venue.

Remember, that each model will result in a single table, and accessing each venue with its address will join both tables, resulting in poor performance over time.

Regarding your error, it might come from your 1-N relation between Venue and Addres. You should have a 1-1.

Just put the address in your Venue, and you'll be fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create a prisma model from a SQL view

How to model a tournament in Prisma

How to filter for related objects in Prisma?

Django: How to update a related model when create an other model

Prisma ORM how to create migration

Create related model with Djoser

How to create a shopping cart model with item variations

Prisma2: use user id in related table in create function

Prisma - How to point two fields to same model?

How to made a relation to user model using Prisma?

Create user related info model

How to create nested connection in prisma query?

how to create a optional list field in prisma

How do I create a new model, related to an existing model, using STI?

How to step forward queryset items through related_name to their foreignkey item in another model

How to make sure in Prisma that only 1 item gets updated?

How to create a django model record using get_or_create and select_related

How to create the related field?

How to define a validation regular expression pattern for a prisma model attribute type?

How do I get the model names in a type form? (Prisma)

How to transform Prisma result into custom model before returning to client with NestJS?

Prisma model Generation -

Dynamically access prisma model

how to create a new item related to a function against previous data in C# linq

Unable to create related model within forloop

Waterline: Populate model related object on create (Sails)

Create and Update Model with 2 related Models

Create several instances out of a related model in Django

getting error in laravel related with create input to model