Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
Prisma get data with foreign key in sqlite DB

I have an SqlLite database that contains these models

model Product {
  id                    Int      @id @unique(map: "sqlite_autoindex_products_1") @default(autoincrement())
  label                 String
  description           String?
  price                 Int
  category_id           Int
  thumbnail_url         String?
  categories            Category @relation("categoriesToproducts", fields: [category_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

  @@map("products")
}

model Category {
  id          Int       @id @unique(map: "sqlite_autoindex_categories_1") @default(autoincrement())
  index       Int?
  label       String
  description String?
  products    Product[] @relation("categoriesToproducts")

  @@map("categories")
}

I would like to get products list using :

async allProducts(): Promise<Product[] | null> {
  return this.prisma.product.findMany();
}

It returns Products raw data with category_Id field.

I would like to know if there is a way to get category mapped data inside Category table instead of only the id field ?

I have a solution is to get Category table data than loop on products list then replace category_id with the category object.

But I would like to know if there is a better way to map directly data when making request via Prisma client.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order to query a related model, you have to either use select or include.

prisma.product.findMany({
  include: {
    categories: true
  }
})

// or

prisma.product.findMany({
  select: {
    categories: true
  }
});

Useful documentation links

  • Relations
  • Relation queries
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda