Does anyone know why Prisma doesn't allow to save ...
# orm-help
j
Does anyone know why Prisma doesn't allow to save an array of arrays as JSON (PostgreSQL)?
a
Hey Jawad 👋 Can you tell me more about your schema and how you're trying to save arrays of arrays? I'm able to save arrays of arrays locally.
j
@andrewicarlson Sure, this is the code:
Copy code
menus.forEach((menu) => {
      const menuTreeOrder = (menu.menuTreeOrder ?? []) as Prisma.JsonArray;

      menuTreeOrder.push([menuCategory.id, []]);

      queries.push(
        dataSources.db.prisma.menu.update({
          where: { id: menu.id },
          data: {
            menuTreeOrder,
          },
        })
      );
    });

    if (queries.length) {
      await dataSources.db.prisma.$transaction(queries);
    }
In schema:
menuTreeOrder _Json_[]
Stack trace:
Copy code
"Error: ",
            "Invalid `prisma.menu.update()` invocation:",
            "",
            "{",
            "  where: {",
            "    id: 'b662224e-2a9a-40bd-a0bb-4cd0e390b3c0'",
            "  },",
            "  data: {",
            "    menuTreeOrder: [",
            "      [",
            "        '61404ca4-469d-4694-bae2-49abcf48fe3c',",
            "        []",
            "      ]",
            "    ]",
            "    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
            "  }",
            "}",
            "",
            "Argument menuTreeOrder: Got invalid value ",
            "[",
            "  [",
            "    '61404ca4-469d-4694-bae2-49abcf48fe3c',",
            "    []",
            "  ]",
            "]",
            "on prisma.updateOneMenu. Provided List<List<UUID | List<>>>, expected MenuUpdatemenuTreeOrderInput or Json:",
            "type MenuUpdatemenuTreeOrderInput {",
            "  set?: List<Json>",
            "  push?: Json | List<Json>",
            "}",
            "type MenuUpdatemenuTreeOrderInput {",
            "  set?: List<Json>",
            "  push?: Json | List<Json>",
            "}",
Not sure what's going on but tried this more than 10 times but no luck
However, what works is an array of objects
a
Thanks for the context, @Jawad Sefiani. Have you tried removing the
[]
from
Json
in your schema? For example:
Copy code
/// schema.prisma
menuTreeOrder  Json
Copy code
const data = await prisma.x.create({
        data: {
            data: [
                "array",
                "of",
                "strings",
                [
                    "and",
                    "other",
                    "strings"
                ]
            ]
        }
    });
j
That didn't help either, im getting the same error:
Copy code
"on prisma.updateOneMenu. Provided List<List<UUID | List<>>>, expected MenuUpdatemenuTreeOrderInput or Json:",
            "type MenuUpdatemenuTreeOrderInput {",
            "  set?: List<Json>",
            "  push?: Json | List<Json>",
            "}",
            "type MenuUpdatemenuTreeOrderInput {",
            "  set?: List<Json>",
            "  push?: Json | List<Json>",
            "}",
a
Sorry to hear that. Are you getting that same error after regenerating the Prisma client? When defining
Json[]
in the Schema it's expecting an array of JSON (as you found). If you migrate (or regenerate your prisma client) after removing
[]
and just setting ``menuTreeOrder _Json`_` I expect that to work. You may also consider removing
as Prisma.JsonArray;
as that declaration may not be casting as you desire. See https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts for more details
j
It worked! Thank you!
🙌 1
Just weird that it didn't work with Json[]