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

Migration from nullable jsonb column to non-nullable produces invalid migration #2131

Closed
TETYYS opened this issue Nov 30, 2021 · 2 comments
Closed

Comments

@TETYYS
Copy link

TETYYS commented Nov 30, 2021

When migrating from nullable jsonb column to a non-nullable one, migration tries to execute following statements:

ALTER TABLE "Table" ALTER COLUMN "JsonbCol" SET NOT NULL;
ALTER TABLE "Table" ALTER COLUMN "JsonbCol" SET DEFAULT '';

This fails with error invalid input syntax for type json, as '' is not valid json.


Workaround:

Set default value for the column as valid json explicitly:

modelBuilder.Entity<Table>()
.Property(x => x.JsonbCol)
.HasDefaultValue("{}");
@roji
Copy link
Member

roji commented Dec 1, 2021

@TETYYS thanks for filing this - yeah, it's an interesting case where the CLR default (empty string) isn't a valid value for the column type (json). There's an ongoing discussion in dotnet/efcore#21765 about this area, let's wait and see how it pans out there.

@roji roji added this to the Backlog milestone Dec 29, 2021
@roji roji added blocked bug Something isn't working labels Dec 29, 2021
@roji
Copy link
Member

roji commented Jan 4, 2022

Currently, based on dotnet/efcore#21765 (comment), the right way to deal with this is to edit the scaffolded migration and to manually replace the empty string with {}, as above. Right now this is by-design rather than a hacky workaround, though on the EF Core side we may discuss again what happens when a column is made non-nullable.

Closing this as by-design for now.

@roji roji closed this as completed Jan 4, 2022
@roji roji removed this from the Backlog milestone Jan 4, 2022
@roji roji removed bug Something isn't working blocked labels Jan 4, 2022
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