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

Allow optional fields to be null #591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

honzajerabek
Copy link
Contributor

Hi there, currently the fields that are nullable are only noted as ?, which allows them to be omitted or undefined. This however doesn't allow them to be set null explicitly.

This is needed mainly when updating to set a field to null. Omitting the field causes no change, passing undefined throws an error in sequelize.

We should be able to call model.update({ name: null })

@schtomas
Copy link

schtomas commented May 9, 2022

I have the same issue. If i generate models from database, all nullable fields appear as possibly undifined and it make problems if i want to insert null or select values where is not null it shows type errors. I had to fix it manualy in code and it looks ok now, but if i generate again models i have to fix it again. Will be some fix for this please?

Solution above it solves maybe problem but what if node modules are installed again? We are useing docker and scripts always install modules from scratch so you have to update again this file autogenerator.ts manualy before generating modelas

@aeharake
Copy link

We should be able to call model.update({ name: null })

Who said you can't do that? You can definitely do it! You're missing something for sure, please open an issue and provide an example of your failing code.

@bossvip
Copy link

bossvip commented Jan 7, 2024

We should be able to call model.update({ name: null })

Who said you can't do that? You can definitely do it! You're missing something for sure, please open an issue and provide an example of your failing code.

https://stackoverflow.com/questions/57243477/can-an-optional-parameter-be-null-in-typescript

"strictNullChecks": true is the default in frameworks like Nextjs. You can check the inferred type for the below code in VSCode.

It is possible to add @ts-ignore and pass null values, but that defeats the point of typescript. I had to update the generated model by hand. It is 2024, why is this PR not merged yet?

  • null not allowed
interface TestProps {
  myVal?: string
 }
  • null allowed
interface TestProps {
  myVal?: string | 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

Successfully merging this pull request may close these issues.

None yet

4 participants