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?

findUniqueで「Type '{ user_id: string; }' is not assignable to type 'User_skillWhereUniqueInput'.」が出た時の解決方法【Prisma】

0
Posted at

はじめに

Prismaを使っていたらfindUniqueで型エラーが出ました。

問題

発生したエラー

Type '{ user_id: string; }' is not assignable to type 'User_skillWhereUniqueInput'.
Type '{ user_id: string; }' is not assignable to type 
'{ id: number; } & { id?: number | undefined; AND?: User_skillWhereInput | User_skillWhereInput[] | undefined; OR?: User_skillWhereInput[] | undefined; NOT?: User_skillWhereInput | ... 1 more ... | undefined; user_id?: string | ... 1 more ... | undefined; skill_id?: number | ... 1 more ... | undefined; }'.
Property 'id' is missing in type 
'{ user_id: string; }' but required in type '{ id: number; }'.

原因となったコード

  const user_skill = await prisma.user_skill.findUnique({
    where: { user_id: user_id },
  });

解決方法

findFirstを使う

  const user_skill = await prisma.user_skill.findFirst({
    where: { user_id: user_id },
  });

おわりに

whereの条件に使っているuser_idは本来ユニークなのですが、このテーブルではユニークとして設定していませんでした。そのためfindFirstを使用しました。ユニークと定義していないカラムに対してfindUniqueは使えないようです。
今回の場合はスキーマを変更した方がいいのかもしれません。

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?