Unknown argument `contains` with prisma

Trying to find records that contain certain words in various fields.

Unknown argument contains. Available options are marked with ?.

clientVersion: ‘5.11.0’

The error doesn’t show any errors marked with ?. RW v 6.x

Is there a more efficient way or a redwood way of doing a term search like this? Does Redwood/prisma support nested AND/OR operations like this?

const conditions = searchWords.map((word) => ({
    OR: [
      { description: { contains: word, mode: 'insensitive' } },
      { inputs: { contains: word, mode: 'insensitive' } },
      { outputs: { contains: word, mode: 'insensitive' } },
    ],
  }))

  const whereClause = {
    app: {
      id: appId,
    },
    AND: conditions,
  }

  const actions = await db.action.findMany({
    where: whereClause,
    select: {
      id: true,
      name: true,
      description: true,
      inputs: true,
      outputs: true,
    },
  })

I sometimes cannot remember the exact Prisma syntax, so I just ask ChatGPT.

The query you’ve written is almost correct, but there might be an issue with how the AND condition is being combined with the search word conditions. Each OR block inside conditions should be combined using AND, so all conditions are met simultaneously.

Here’s the fixed version:

const conditions = searchWords.map((word) => ({
  OR: [
    { description: { contains: word, mode: 'insensitive' } },
    { inputs: { contains: word, mode: 'insensitive' } },
    { outputs: { contains: word, mode: 'insensitive' } },
  ],
}));

const whereClause = {
  app: {
    id: appId,
  },
  AND: conditions,
};

const actions = await db.action.findMany({
  where: whereClause,
  select: {
    id: true,
    name: true,
    description: true,
    inputs: true,
    outputs: true,
  },
});

Explanation:

  • The conditions array is mapped correctly with each word having an OR condition for description, inputs, and outputs.
  • The AND clause combines all these OR conditions, which ensures that actions are only selected if they match all the search terms provided in searchWords.

This should now work as intended in your Prisma query.

Does that help?

Or in might be:

The error you’re encountering suggests that the fields you’re trying to query don’t support the contains operation directly, or that there might be some issue with the field types or how the query is structured.

Here are a few things to check and alternative approaches:

1. Check the Field Types:

  • Ensure that the description, inputs, and outputs fields are of a type that supports the contains operation. Typically, contains works with String or String[] fields. If these fields are not strings, you might get this error.

2. Use search Instead:

  • If you are using a field that does not support contains, you may try using search if available, which is used for full-text search on fields that are indexed for it.

3. Check for Typos or Version Issues:

  • Ensure that the version of Prisma you are using supports the contains filter on the field types you’re querying. Sometimes, the API may change between versions, or there might be some breaking changes.

4. Array of Strings:

  • If the field is an array of strings (String[]), you should check whether contains can be applied to each item in the array.

5. Possible Fix with String Arrays:

  • If you’re dealing with an array of strings and need to match any element in the array containing the word, you could approach it like this:

Your Prisma client is quite old at 5.11. latest in 5.18. I’d have to check release notes to see if Prima changes syntax.

Alternatively, you could save a repo where we could reproduce this message. Thanks.

1 Like