Skip to content

Commit d095e83

Browse files
fix: only exclude nullable columns from DataTable PK when they have a default
Fixes a bug where non-nullable columns with DEFAULT constraints were incorrectly removed from the DataTable PrimaryKey. This caused ConstraintException when inserting rows that differ only in the column that was wrongly excluded from the PK. Root cause: the original condition checked only for the presence of a DEFAULT constraint, but the comment described the intent as preventing null PK values. For non-nullable columns, callers must always supply the value, so including them in the PK is correct. Fix: add the c.Nullable guard so that only nullable+default columns are removed from the PK. Non-nullable columns with defaults retain PartOfUniqueKey = true, so the generated DataTable constraint correctly enforces uniqueness across all PK columns. Closes #376 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ac79dd6 commit d095e83

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/SqlClient.DesignTime/SqlClientProvider.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,13 @@ type SqlProgrammabilityProvider(config : TypeProviderConfig) as this =
295295
?defaultValue = x.TryGetValue("default_constraint"),
296296
?description = x.TryGetValue("description")
297297
)
298-
if c.DefaultConstraint <> "" && c.PartOfUniqueKey
298+
if c.DefaultConstraint <> "" && c.Nullable && c.PartOfUniqueKey
299299
then
300300
{ c with PartOfUniqueKey = false }
301-
//ADO.NET doesn't allow nullable columns as part of primary key
302-
//remove from PK if default value provided by DB on insert.
301+
//ADO.NET doesn't allow null values in primary key columns.
302+
//Only remove nullable columns from PK when they have a default value,
303+
//since nullable+default columns may be omitted in NewRow() and remain null.
304+
//Non-nullable columns with defaults should stay in PK - callers must supply them.
303305
else c
304306
)
305307
|> Seq.toList

0 commit comments

Comments
 (0)