0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【prisma】 findMany メソッドのオプション

Last updated at Posted at 2024-09-23

includeとselectは併用できない
prismaで文字列連結はできない

サンプルDB

リレーション(1対多、多対多)のあるサンプル

prisma type attribute sample

  • @default(now()): デフォルトで現在時刻を挿入
  • @updatedAt: {insert | update} 時に現在時刻を挿入
schema.prisma
// ユーザーテーブル
model Users {
  U_id                   Int       @id @default(autoincrement())
  U_name           String
  U_birth_date           DateTime? @db.Date()
  U_gender               String?   @db.Char(1)
  U_role_id              Int
  U_created_at           DateTime  @default(now())
  U_updated_at           DateTime  @updatedAt
  U_state                Int
  // リレーションを定義
  user_groups  User_groups[]
  role         Roles  @relation(fields: [role_id], references: [R_id])
}

// ユーザー・グループ中間テーブル
model User_groups {
  user_id   Int  @id
  group_id  Int  @id
  // リレーションを定義
  user   Users   @relation(fields: [user_id], references: [U_id])
  group  Groups  @relation(fields: [group_id], references: [G_id])
}

// グループテーブル
model Groups {
  G_id          Int       @id @default(autoincrement())
  G_name        String
  G_created_at  DateTime  @default(now())
  G_updated_at  DateTime  @updatedAt
  G_deleted     Boolean   @db.Bit(1)
  // リレーションを定義
  user_groups  User_groups[]
}

// 役職テーブル
model roles {
  R_id          Int       @id @default(autoincrement())
  R_name        String
  R_created_at  DateTime  @default(now())
  R_updated_at  DateTime  @updatedAt
  R_deleted     Boolean   @db.Bit(1)
  // リレーションを定義
  users  Users[]
}

SELECT 文

WHERE, JOIN

Prismaのクエリ

prisma.users.findMany({
  where: {
    U_state: 1
  },
  select: {
    U_id: true,
    U_name: true,
    role: {
      select: {
        R_name: true
      }
    }
  }
});

生成されるMySQLのSQL

SELECT
  u.U_id,
  u.U_name,
  r.R_name AS role_name,
FROM
  Users u
LEFT JOIN
  roles d ON u.U_role_id = r.R_id
WHERE
  u.U_state = 1;

多対多で2つ先のテーブルまで参照

Prismaのクエリ

prisma.users.findUnique({
  where: {
    U_id: 1,
    U_state: 1
  },
  select: {
    U_id: true,
    U_name: true,
    role: {
      select: {
        R_name: true,
      }
    },
    user_groups: {
      select: {
        group: {
          select: {
            G_name: true,
          }
        }
      }
    }
  }
});

生成されるMySQLのSQL

SELECT
  u.U_id,
  u.U_name,
  r.R_name AS role_name,
  g.G_name AS group_name
FROM
  Users u
LEFT JOIN
  roles r ON u.U_role_id = r.R_id
LEFT JOIN
  user_groups ug ON u.U_id = ug.U_id
LEFT JOIN
  groups g ON ug.group_id = g.G_id
WHERE
  u.U_id = 1 AND u.U_state = 1;

References

おわりに

他の Prisma 記事です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?