Skip to content

Can't create a record with only the foreign key to another table #2152

@cannikin

Description

@cannikin

Bug description

You are unable to create a new record by using the foreign key to another table. For example if a Post has a userId column, you cannot just set the userId and save the record.

How to reproduce

Given the schema shown below, create a User and note its ID. Let's say it is 1 in this case. Now try to create a post:

db.post.create({ data: { title: 'My post', body: 'Lorem ipsum...', userId: 1 } })

Error:

Invalid `prisma.post.create()` invocation:
{
  data: { 
    title: 'First post',    
    body: 'Body goes here',    
    userId: 1,   
    ~~~~~~
    +   user: {
    +     create?: UserCreateWithoutPostsInput,
    +     connect?: UserWhereUniqueInput
    +   }  
  }
}
Unknown arg `userId` in data.userId for type PostCreateInput. Did you mean `user`?
Argument user for data.user is missing.

Expected behavior

The Post record is created and userId is set to 1.

Prisma information

// schema.prisma
datasource DS {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id     Int     @id @default(autoincrement())
  title  String  @unique
  body   String
  user   User    @relation(fields: [userId], references: [id])
  userId Int
}

Environment & setup

rob$ yarn prisma -v
yarn run v1.22.4
$ /Users/rob/Sites/redwoodjs/abbott/node_modules/.bin/prisma -v
@prisma/cli          : 2.0.0-beta.1
Current platform     : darwin
Query Engine         : prisma 2accb9c7eacdc984874eaeb63377fe705dfd3203 (at /Users/rob/Sites/redwoodjs/abbott/node_modules/@prisma/cli/query-engine-darwin)
Migration Engine     : migration-engine-cli 2accb9c7eacdc984874eaeb63377fe705dfd3203 (at /Users/rob/Sites/redwoodjs/abbott/node_modules/@prisma/cli/migration-engine-darwin)
Introspection Engine : introspection-core 2accb9c7eacdc984874eaeb63377fe705dfd3203 (at /Users/rob/Sites/redwoodjs/abbott/node_modules/@prisma/cli/introspection-engine-darwin)
rob$ node -v
v13.8.0

Why this is important

With RedwoodJS we have generators that create simple CRUD pages for working the data in your database. In this case we would create a "Post" form to let you create new ones. To associate the new post to a user we provide a userId input. Ideally the user just enters the ID of the user and they're done:

image

Unfortunately this behavior breaks that completely. :( We would need a lot of code to work around this (perform an extra database query to look up the user and then include that user using the connect syntax, I guess?). This can all be avoided by just letting users set all the fields in the database table, which most would expect to be able to do.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions