Skip to content

Conversation

maumar
Copy link
Contributor

@maumar maumar commented Feb 9, 2024

Problem was that when adding new required column to an existing table we always need to provide default value (to fill the values for rows already in the table). For collection of primitives that get map to json we should be setting the value to empty JSON collection, i.e. '[]' but we were not doing that.

Fixes #32972

@maumar maumar requested a review from a team February 9, 2024 09:03
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we do this already for JSON columns mapped to owned entity types?

@maumar
Copy link
Contributor Author

maumar commented Feb 9, 2024

yes, we already do this for JSON InitializeJsonColumn. Only need to do this for references - JSON collections can never be required

…ive collections mapped to JSON is invalid

Problem was that when adding new required column to an existing table we always need to provide default value (to fill the values for rows already in the table). For collection of primitives that get map to json we should be setting the value to empty JSON collection, i.e. '[]' but we were not doing that.

Fixes #32972
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this implementation is in line with how things are currently done, but I'm wondering whether implementing the default value in the differ (as opposed to the migrations SQL generator) as the right place... Doesn't this mean that we're setting the default value even when simply a table is created with a JSON collection (as opposed to a collection column being added to an existing table)? In that case having a default value doesn't seem appropriate...

In other words, should this all happen in the AddColumn case in SqlMigrationsGenerator instead?

{
defaultValue = null;
// for non-nullable collections of primitives that are mapped to JSON we set a default value corresponding to empty JSON collection
defaultValue = !inline
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this logic move into GetDefaultValue instead, which is the code responsible for determining default values? It would have to accept the column as opposed to just the CLR type, of course.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks messy because it's conflating the default value that should be used when a new row is added with the default value that should be used when a new required column is added or an existing column is made required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji This is a long standing issue with the way Migrations uses default values. See #27084.

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.

The database default created by Migrations for primitive collections mapped to JSON is invalid

4 participants