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

Adding a new (non-nullable) JSON column populates it with an empty string (invalid JSON) #32353

Closed
roji opened this issue Nov 20, 2023 · 1 comment · Fixed by #32354
Closed
Assignees
Labels
area-json area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug

Comments

@roji
Copy link
Member

roji commented Nov 20, 2023

When adding a new column, we add it with a column DEFAULT for the CLR default of the type (see this code in MigrationsModelDiffer); this means that the JSON column default is an empty string:

ALTER TABLE [Blogs] ADD [JsonDetails] nvarchar(max) NOT NULL DEFAULT N'';

For PostgreSQL, this fails since the column is typed as jsonb, and an empty string isn't a valid JSON document. I've worked around this in the migrations SQL generator (PR), but that's not the right place for this - it notably leaves the empty string in the scaffolded migration code.

This doesn't error on e.g. SQL Server because the column type is nvarchar(max), but we end up with invalid data in the column. I'm not sure if this can cause a real bug, but it certainly could.

Note relationship with #28596.

/cc @maumar

roji added a commit to roji/efcore that referenced this issue Nov 20, 2023
@roji roji self-assigned this Nov 20, 2023
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 20, 2023
@roji
Copy link
Member Author

roji commented Nov 20, 2023

Note that to work around this in Npgsql, I considered temporarily overriding MigrationsModelDiffer to do #32354 in the provider. But tons of stuff there is private, so it wasn't possible to override that particular code within copying in really large quantities of the differ. That isn't great, since removing the migrations SQL generator hack I did in npgsql/efcore.pg#2967 would be a breaking change (people will already have scaffolded migrations with an empty string as the default).

@ajcvickers ajcvickers added this to the 9.0.0 milestone Nov 23, 2023
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-json area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants