-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
SQL Server Migrations: Handle AlterColumn(nullable: false) #21765
Comments
Note, this only fails when the column actually contains nulls. |
This also fails when attempting to add a default value to a previously nullable column (Azure SQL, EF Core 5.0). Current Model:
Desired Model:
Migration:
Generated SQL:
It seems that the simple fix would be to have default values have precedence over the |
Same here, but in my case, the column I want to make non-nullable has no null values, but it still fails to migrate. This seems different from what is described by @bricelam : #21765 (comment) |
Are we sure it's a good idea to implicitly/silently convert NULLs to the C# default? It may be safer to require the user to do the explicit gesture of including the above, so they're clear on the decision they're making (after all, the database itself doesn't do this implicitly, so I'm not sure we should either). |
The original idea was that we'd generate a data loss warning, then you'd review the migration to specify the value you wanted. We just use the CLR default as a starting point since this is the value that would be there if the property was required when the entity was inserted. |
Note npgsql/efcore.pg#2131 which was just opened, and where the CLR default (empty string) is an invalid value for the column type (json). Of course, we could allow providers to specify default values for type mappings, but this may also point towards forcing users to deal with this up-front rather than trying to do this for them. A data loss warning may be good enough, though I think there's a difference between more classical, clearly-intentional data loss scenarios (you removed a property) and this more subtle scenario - I'm usually a fan of just exposing the database to users without doing any magic, and databases simply don't allow this. Am also not sure users would realize the possible danger just by seeing the proposed UPDATE in the migration code - maybe a specific/explicit warning would help with that. |
Found this while working on #19882. When changing a property to required (aka NOT NULL), the database currently throws.
We could handle this better by translating it as follows.
The text was updated successfully, but these errors were encountered: