Search code examples
c#postgresqldatatablenpgsql

DataTable constraint error when loading null value from NpgSQL


I have a C# application that uses NpgSQL to communicate with a postgres database. However, when I try to fill a DataTable with a result containing a NULL value, an error is thrown:

"System.Data.ConstraintException: 'Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.'"

This is even when I do something like:

NpgsqlCommand statement = new NpgsqlCommand("SELECT NULL", connection);
NpgsqlDataReader resultReader = statement.ExecuteReader();
var table = new DataTable();
table.Load(resultReader);

In the Visual Studio command window, table.GetErrors()[0]` gives:

{System.Data.DataRow}
    HasErrors: true
    ItemArray: {object[1]}
    RowError: "Column '?column?' does not allow DBNull.Value."
    RowState: Unchanged
    Table: {}

Can this be related to this bug?


Solution

  • The problem was caused by our project using an old version of NpgSQL (3.2.2). As of version 3.2.3, this problem has been solved.