【Prisma】条件判断中,数组包含如何进行判断?

The error occurs because you are using the has operator on a field typed as Json in your Prisma schema. In Prisma, the has operator is only available for Scalar Lists (native database arrays like Int[] or String[]).

For a Json field, the operator you are looking for is array_contains.

Here are the two ways to fix this:

Option 1: Use array_contains (If field remains Json)

If your ganyu_options field must stay as a Json type in your schema.prisma, change your code to this:

typescript 复制代码
where: {
  OR: [
    { type: 1 },
    { 
      ganyu_options: { 
        array_contains: low_number // Use array_contains for Json fields
      } 
    },
  ],
  status: 1,
},

Note: array_contains works on PostgreSQL and MySQL (for JSONB/JSON columns).


Option 2: Change Schema to Scalar List (Recommended)

If ganyu_options is always going to be a list of numbers, it is better to define it as an array in your schema.prisma. This makes it type-safe and allows you to use the has operator.

  1. Update your schema.prisma:

    prisma 复制代码
    model MiniArticle {
      id            Int    @id @default(autoincrement())
      // Change from Json to Int[]
      ganyu_options Int[]  
      status        Int
      type          Int
    }

    (Note: Native scalar lists like Int[] are supported out-of-the-box by PostgreSQL and CockroachDB. If you are using MySQL, you must stick with Option 1).

  2. Run migration:

    bash 复制代码
    npx prisma migrate dev
  3. Your original code will now work:

    typescript 复制代码
    where: {
      OR: [
        { type: 1 },
        { ganyu_options: { has: low_number } }, // This works now!
      ],
      status: 1,
    },

Summary: Which one to choose?

  • If using PostgreSQL and the data is always a list: Use Option 2 (Change to Int[]). It is faster and more type-safe.
  • If using MySQL or the JSON structure is unpredictable: Use Option 1 (array_contains).
相关推荐
亮子AI4 小时前
【Prisma】如何修复(重建)已经损坏的迁移历史?
数据库·prisma
雪域迷影17 天前
完整的后端课程 | NodeJS、ExpressJS、JWT、Prisma、PostgreSQL
数据库·postgresql·node.js·express·prisma
喜欢踢足球的老罗1 个月前
Sequelize vs Prisma:现代 Node.js ORM 深度技术解析与实战指南
node.js·prisma·sequelize
江湖人称小鱼哥2 个月前
Prisma 命令安全指南
数据库·安全·prisma
濮水大叔4 个月前
Node.js 主流ORM框架动态分表方案大盘点
typescript·nodejs·orm·prisma
YUELEI1181 年前
Prisma ORM 第四章 表之间关系的建立和删除
prisma
YUELEI1181 年前
Prisma ORM 第一章 安装与使用
prisma
白雾茫茫丶1 年前
前端的全栈之路:基于 Vue3 + Nest.js 全栈开发的后台应用
postgresql·vue3·vite·pinia·nest.js·prisma