Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolds don't handle optional relations with string keys #4297

Closed
Tobbe opened this issue Jan 29, 2022 · 2 comments · Fixed by #4302
Closed

Scaffolds don't handle optional relations with string keys #4297

Tobbe opened this issue Jan 29, 2022 · 2 comments · Fixed by #4302

Comments

@Tobbe
Copy link
Member

Tobbe commented Jan 29, 2022

Given this prisma schema

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  addressId String?
  address   Address? @relation(fields: [addressId], references: [id])
}

model Address {
  id           String  @id @default(uuid())
  country      String
  state        String?
  zip          String
  town         String
  street       String
  streetNumber String // Can be 12C for example
  users        User[]
}

If you scaffold user you get this

image

Trying to create a new user like this you get an error

image

The front-end says {"errors":[{"message":"Something went wrong."}],"data":null}
The back-end says

api | Invalid `db.user.create()` invocation in
api | /Users/tobbe/dev/redwood/scaffoldnull/api/src/services/users/users.js:14:18
api | 
api |   11 }
api |   12 
api |   13 export const createUser = ({ input }) => {
api | → 14   return db.user.create(
api |   Foreign key constraint failed on the field: `foreign key`

But it should be fine to leave the id out, because the relation is optional

@dthyresson
Copy link
Contributor

dthyresson commented Jan 30, 2022

Adding a validate to those TextFields works:

        <TextField
          name="purchaserId"
          defaultValue={props.possession?.purchaserId}
          className="rw-input"
          errorClassName="rw-input rw-input-error"
          validation={{
            setValueAs: (val) => {
              if (val === '') {
                return null
              }
            },
          }}
        />

Might be nice to have a setEmptyAsNull: true option on TextField and SelectList

@Tobbe
Copy link
Member Author

Tobbe commented Jan 30, 2022

Have a PR ready to go, just need #4299 to be merged first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants