Hi, I’m getting an error when I try and seed my db...
# prisma-migrate
a
Hi, I’m getting an error when I try and seed my db with a nested create. It works if I only add one row in the nested table. But if try and pass an array of rows, it fails. details in the thread
the schema is the following..
Copy code
model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  lists List[]
}

model List {
  id        Int     @id @default(autoincrement())
  name     String
  links   String[]
  ownerId  Int?
  owner    User?   @relation(fields: [ownerId], references: [id])
}
I have the following seed data
Copy code
[
  { "email" : "<mailto:athir@test.com|athir@test.com>",
    "lists" : [
      { 
        "name" : "Italy", 
        "links" : [
          "<https://adventuresoflilnicki.com/southern-italy-road-trip>",
          "<https://adventuresoflilnicki.com/things-to-do-matera-italy/>",
          "<https://adventuresoflilnicki.com/syracuse-travel-sicily/>",
          "<https://eternalarrival.com/rome-itinerary-3-days/>"
      ]}
    ]
  },
  { "email" : "<mailto:nancy@test.com|nancy@test.com>",
    "lists" : [
      {
        "name" : "Mexico City", 
        "links" : [
          "<https://www.geekyexplorer.com/mexico-city-itinerary/>",
          "<https://www.washingtonpost.com/travel/mexico/mexico-city-local-guide/>"
        ]
      }
    ]
  }
]
here is the code for seed.ts
Copy code
// read data from json file
  const seed_data = <User[]>require("../data/seed.json");

  // go through each user
  for (const user_data of seed_data) {    
    // user lists, need 'create' item
    let user = await prisma.user.create({
      data: {
        email: user_data['email'],
        lists: {
          create: user_data['lists'],
        }
      },
    });
The error I get is
Copy code
25 console.log(user_data)
  26
  27 // user lists, need 'create' item
→ 28 let user = await prisma.user.create(
  The column `links` does not exist in the current database.
If I change
user_data['lists']
to be just a single element
user_data['lists'][0]
it works
m
You would need to use nested
createMany
instead of
create
. Try something like this:
Copy code
for (const user_data of seed_data) {    
    // user lists, need 'create' item
    let user = await prisma.user.create({
      data: {
        email: user_data['email'],
        lists: {
          createMany: {
            data: user_data['lists'],
         }
        }
      },
    });
a
will try that. The current approach uses an array (user_data) as there is an example on the prisma website that does it.
a
thanks. will try that
m
I see, yeah
a
I’m wondering if the issue is that the column in question is a
string[]
. I’m not sure what that maps to in postgres
Is there a way to see the raw sql that prisma uses to create the db?
m
According to docs, your way should work too, yes.
a
I’m using supabase as the backend. If I use their UI to manually add a record to the table, I get the same error,. Supabase has the column
links
as a text array. Did not know postgres has that. I think this is schema issue rather than a create() issue
I’m going to dig into that now
OK. definitely something funky on supabase side. One UI has the
links
column and another does not (older schema). I’m going to delete the database and recreate it and then dig into the
strings[]
issue.