| Detailed description |
Npgsql Version: 1.99.2.0
PostgreSQL Version: 8.3.4
Repro Steps:
1) On Postgres, create an ENUM data type:
CREATE TYPE OrderStateEnum AS ENUM ('Preview', 'InQueue', 'Ordered', 'Error', 'Cancelled');
2) Next, create a table that uses this type:
CREATE TABLE test
(
id integer NOT NULL,
state orderstateenum
) WITH (OIDS=FALSE);
3) Insert a row into this table:
insert into test values (1, 'InQueue');
4) Using Npgsql, connect to DB and run the following code. I'm using NHibernate/Castle, but this shouldn't affect the results.
ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
ISession session = holder.CreateSession(typeof(DB.User));
IDbCommand cmd = session.Connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * From test";
IDataReader reader = cmd.ExecuteReader();
5) Results:
reader.FieldCount is 2.
reader.Read(); returns true
reader.GetValues() returns correct values, with state column as a string. This is expected.
6) Add the following code:
DataSet ds = new DataSet();
ds.Load(reader, LoadOption.OverwriteChanges, "test");
7) Results:
Exception thrown:
{"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
Stack trace:
at System.Data.DataSet.FailedEnableConstraints()
at System.Data.DataSet.EnableConstraints()
at System.Data.DataSet.set_EnforceConstraints(Boolean value)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataSet.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler, DataTable[] tables)
at System.Data.DataSet.Load(IDataReader reader, LoadOption loadOption, String[] tables)
...
8) Change SELECT statement to:
cmd.CommandText = "SELECT id From test";
9) Results:
Dataset now loads fine.
EXPECTED:
DataSet should populate with System.String data type for "state" field.
WORK AROUND:
Enum can be casted to text to work around this bug. However, this forces database to alter its preferred design to accomodate this bug. I expect enum types should behave as strings in all cases. |
|