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

Does a generated Insertable property for a nullable column have to be optional? #169

Open
adrianmisko opened this issue Apr 13, 2024 · 1 comment

Comments

@adrianmisko
Copy link

Given a schema that has a nullable column:

create table authors(name varchar(255) primary key, stage_name varchar(255));

The generated insertable is:

    export interface Insertable {
      /**
      * **authors.name**
      * - `varchar` in database
      * - `NOT NULL`, no default
      */
      name: string | db.Parameter<string> | db.SQLFragment;
      /**
      * **authors.stage_name**
      * - `varchar` in database
      * - Nullable, no default
      */
      stage_name?: string | db.Parameter<string> | null | db.DefaultType | db.SQLFragment;
    }

Does the stage_name property have to be optional? This causes the following code to pass:

  const authors: s.authors.Insertable[] = [
    { name: 'Homer' },
  ]
  db.insert('authors', authors).run(pool);

even when using exactOptionalPropertyTypes.

I might forget to include an optional property and the compiler won't complain. That would not be the case if I the property was:

      stage_name:
        | string
        | db.Parameter<string>
        | null
        | db.DefaultType
        | db.SQLFragment

Then, an explicit null would be required:

  const authors: s.authors.Insertable[] = [
    { name: 'Homer', stage_name: null },
  ]

Is there a reason generated properties for Insertables are optional? I know that there are cases where you'd want that - generated values, values set by triggers. However, I think it should be null for "usual" nullable column. I don't know about backwards compatibility though.

Kinda related to #25

@remidewitte
Copy link

If you need this behavior, you might just use Required<Insertable>.

const authors: Required<s.authors.Insertable>[] = [
    { name: 'Homer', stage_name: null },
  ]

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

No branches or pull requests

2 participants