Skip to content

Commit

Permalink
No longer scaffold nullable booleans when column has a default value
Browse files Browse the repository at this point in the history
Fixes #13613
  • Loading branch information
ajcvickers committed Jun 27, 2023
1 parent e88e4d1 commit 41b35b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ protected virtual EntityTypeBuilder VisitColumns(EntityTypeBuilder builder, ICol
}

if (clrType == typeof(bool)
&& column.DefaultValueSql != null)
&& column.DefaultValueSql != null
&& column.DefaultValue == null)
{
_reporter.WriteWarning(
DesignStrings.NonNullableBoooleanColumnHasDefaultConstraint(column.DisplayName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ public void Pluralization_of_collection_navigations_noPluralize()
}

[ConditionalFact]
public void Not_null_bool_column_with_default_value_is_made_nullable()
public void Not_null_bool_column_with_unparsed_default_value_is_made_nullable()
{
var dbModel = new DatabaseModel
{
Expand Down Expand Up @@ -1910,6 +1910,60 @@ public void Not_null_bool_column_with_default_value_is_made_nullable()
Assert.Equal("Default", columns.First(c => c.Name == "NonNullBoolWithDefault")[RelationalAnnotationNames.DefaultValueSql]);
}

[ConditionalFact]
public void Not_null_bool_column_with_parsed_default_value_is_not_made_nullable()
{
var dbModel = new DatabaseModel
{
Tables =
{
new DatabaseTable
{
Database = Database,
Name = "Table",
Columns =
{
IdColumn,
new DatabaseColumn
{
Table = Table,
Name = "NonNullBoolWithDefault",
StoreType = "bit",
DefaultValueSql = "1",
DefaultValue = true,
IsNullable = false
},
new DatabaseColumn
{
Table = Table,
Name = "NonNullBoolWithoutDefault",
StoreType = "bit",
IsNullable = false
}
},
PrimaryKey = IdPrimaryKey
}
}
};

var model = _factory.Create(dbModel, new ModelReverseEngineerOptions());

var columns = model.FindEntityType("Table")!.GetProperties().ToList();
var columnWithDefault = columns.First(c => c.Name == "NonNullBoolWithDefault");
var columnWithoutDefault = columns.First(c => c.Name == "NonNullBoolWithoutDefault");

Assert.Equal(typeof(bool), columnWithoutDefault.ClrType);
Assert.False(columnWithoutDefault.IsNullable);
Assert.Equal(typeof(bool), columnWithDefault.ClrType);
Assert.False(columnWithDefault.IsNullable);
Assert.Equal("1", columnWithDefault[RelationalAnnotationNames.DefaultValueSql]);
Assert.Equal(true, columnWithDefault[RelationalAnnotationNames.DefaultValue]);
Assert.Null(columnWithoutDefault[RelationalAnnotationNames.DefaultValueSql]);
Assert.Null(columnWithoutDefault[RelationalAnnotationNames.DefaultValue]);

Assert.Empty(_reporter.Messages);
}

[ConditionalFact]
public void Nullable_column_with_default_value_sql_does_not_generate_warning()
{
Expand Down

0 comments on commit 41b35b5

Please sign in to comment.