Prisma*
# orm-help
a
Prisma*
r
for Prisma that works with databases, this would be the correct Slack group
a
Okay i don’t really know how to use slack so
r
It’s fine, you can ask your question here directly 🙂
a
I am having an issue with prisma itself/I dont fully understand how to use it.
I keep getting errors when trying to create an entry in the database.
r
Could you share what you’ve tried so far?
a
Sure let me reopen vscode
I will explain my issue further while I wait.
I am trying to create a one-to-many relation between one table and the other.
I ran into the issue of trying to create an entry for one table that will correspond with the other
Copy code
prisma.warning.create({
                data: {
                    userId: Number(user.id),
                    modId: Number(msg.author.id),
                    reason: reason,
                    time: time,
                    user: {
                        connect: { userId: Number(user.id) }
                    }
                }
            })
That didn't turn out right but that is what I tried so far.
Copy code
model User {
  userId   Int       @unique
  rep      Int
  xp       Int
  level    Int
  warnings Warning[]
}

model Warning {
  id         Int      @id @default(autoincrement())
  reason     String
  modId      Int
  time       String
  user       User
  userId Int
}
Those are my two models
Let me also get the error
r
I think the models that you have created are a bit incorrect:
Copy code
model User {
  userId   Int       @unique
  rep      Int
  xp       Int
  level    Int
  warnings Warning[]
}

model Warning {
  id     Int    @id @default(autoincrement())
  reason String
  modId  Int
  time   String
  user   User   @relation(fields: [userId], references: [userId])
  userId Int
}
This should be the correct relation. You can also read more about creating relations here. And the way I created a warning is as follows:
Copy code
const data = await prisma.warning.create({
    data: {
      modId: 1,
      reason: 'reason',
      time: 'time',
      user: {
        connect: { userId: 1 },
      },
    },
  })
a
Hm, Alright I tried all that and I got an error in my prisma studio terminal
Copy code
Error in request:  PrismaClientUnknownRequestError: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("42703"), message: "column Warning.userUserId does not exist", detail: None, hint: None, position: Some(Original(146)), where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("parse_relation.c"), line: Some(3284), routine: Some("errorMissingColumn") }) }) })
    at PrismaClientFetcher.request (C:\Users\the_c\AppData\Roaming\Prisma\Studio\-Users-the_c-OneDrive-Desktop-LateNight\runtime\index.js:1:226583)
    at processTicksAndRejections (internal/process/task_queues.js:94:5) {
  query: 'prisma.warning.findMany({\n' +
    '  take: 100,\n' +
    '  skip: 0,\n' +
    '  select: {\n' +
    '    id: true,\n' +
    '    reason: true,\n' +
    '    modId: true,\n' +
    '    time: true,\n' +
    '    user: true,\n' +
    '    userId: true,\n' +
    '    userUserId: true,\n' +
    '  }\n' +
    '})'
}
I dont see why I would have a userUserId column
r
Could you clear the database and migrations and start from scratch
a
How do I do that?
Do I just delete the stuff in the migrations folder?
r
Yes the entire migrations folder, and possible the database as well if you do not have important data
a
Alright I deleted the migration folder but it wont let me wipe the database
r
What DB are you using? Postgres?
a
Yes, sir.
r
The use a database client like PGAdmin or TablePlus to delete the entire database
a
Alright.
Alright I am using pgadmin but my database isn't popping up. It is telling me I have 770 databases
r
Did you run these comands before running your query?
Copy code
prisma migrate save --experimental
prisma migrate up --experimental
These commands create the database and add the migrations
a
Yes I did.
But I am trying to wipe my old database.
So the thing is I am hosting my dev database with elephantsql so it is hard for me to actually find my database since when you login with pgadmin it shows every database they host.
r
Why not search directly for the db name?
a
I eventually switched to dbeaver and managed to wipe the database.
So the migrations and database are wiped.
Do I now migrate again or?
r
Yes migrate again with the schema I provided
And then run the code that I gave for adding a warning
a
Well I think that is an issue.
I migrated and it brought back the old migrations.
r
This means you didn’t delete the DB or the migrations folder
a
I did delete the migrations folder and wiped the database as well completely.
There are no tables in the database/
r
try changing the database name then before migrating
a
I cant really change the name of the database when hosting with elephantsql it picks one at random
Alright I think I got it.
The old migrations no longer come back. There is only now 1 migration which is the one I just did.
Copy code
PrismaClientKnownRequestError: 
Invalid `prisma.warning.create()` invocation in
C:\Users\the_c\OneDrive\Desktop\LateNight\commands\Mod\test.js:33:34


  Query interpretation error. Error for binding '1': AssertionError("[Query Graph] Expected a valid parent ID to be present for a nested connect on a one-to-many relation.")
    at PrismaClientFetcher.request (C:\Users\the_c\OneDrive\Desktop\LateNight\node_modules\@prisma\client\runtime\index.js:1:226407)
    at processTicksAndRejections (internal/process/task_queues.js:94:5) {
  code: 'P2016',
  meta: {
    details: `Error for binding '1': AssertionError("[Query Graph] Expected a valid parent ID to be present for a nested connect on a one-to-many relation.")`
  }
}
I get this error now. Which is the error I used to get.
r
I guess it means the User id is not present that you are connecting it to. Could you try creating the User first then connecting it to the warning?
Something like:
Copy code
await prisma.user.create({
    data: {
      level: 1,
      rep: 1,
      userId: 1,
      xp: 1,
    },
  })
  const data = await prisma.warning.create({
    data: {
      modId: 1,
      reason: 'reason',
      time: 'time',
      user: {
        connect: { userId: 1 },
      },
    },
  })
a
Same error