Skip to content

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

@roji

Description

@roji

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

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions