Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma client select all rows from a table

how can I select everything from a table with prisma? (SELECT * FROM application)

const applications = prisma.application.findMany({
        // Returns all user fields
        include: {
            posts: {
                select: {
                    age: true,
                    about_section: true,
                    user_id: true
                },
            },
        },
    })
    console.log(applications.age)

Here is how my schema looks:

model application {
  application_id Int     @id @default(autoincrement())
  age            String? @db.VarChar(255)
  about_section  String? @db.VarChar(255)
  user_id        Int?
  users          users?  @relation(fields: [user_id], references: [user_id], onDelete: Restrict, onUpdate: Restrict, map: "application_ibfk_1")

  @@index([user_id], map: "user_id")
}
like image 395
Kraier Avatar asked Feb 24 '26 03:02

Kraier


1 Answers

For rows, findMany() without where will return all rows in table:

Get all Records
The following findMany query returns all User records:

const users = await prisma.user.findMany()

For columns/fields, without specifying include or select, prisma has a default:

By default, when a query returns records (as opposed to a count), the result includes the default selection set:

  • All scalar fields defined in the Prisma schema (including enums)
  • None of the relations

For SELECT * FROM application (does not include users relation):

const applications = await prisma.application.findMany();

To futher include users relation in the model:

const applications = await prisma.application.findMany({
  include: {
    users: true // will include all fields
  }
});
like image 129
Ironolife Avatar answered Feb 26 '26 16:02

Ironolife



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!