Maciej
Maciej

Reputation: 10805

DataAdapter / CommandBuilder - Unique constraint on multiple columns

Im using command builder to auto generate SQL statements and DataAdapter to fill DataSet Generally works well but now I have issue with Unique constraint on multiple columns

I have in DB (PostgreSQL) such unique index

CREATE UNIQUE INDEX idx_uniq ON table_1 (id,currency_id); 

DataAdapter / CommandBuilder read this wrongly and set unique flag on each column separately (id & currency_id), instead use combination of both as unique.

I have huge impact on my app now - because I cant even fill dataset - because it says constrains are broken. Any way to force DA to read that correctly or remove those wrong constrains ?

I stuck with that since few hrs thus any help / tips will be highly appreciated ...

Upvotes: 0

Views: 110

Answers (1)

D Stanley
D Stanley

Reputation: 152556

If this is a flaw in the command builder, your quickest resolution may be to remove the existing constraint and manually add a new one:

UniqueConstraint unique = 
   new UniqueConstraint(new { table_1.Columns["id"], 
                              table_1.Columns["currency_id"] });

// remove all unique constraints except primary key
// use ToList so that we can remove the constraint within the loop
for(c in table_1.Constraints
                .OfType<UniqueConstraint>()
                .Where(uc => !uc.IsPrimaryKey)
                .ToList())
{
    table_1.Constraints.Remove(c);
}
table_1.Constraints.Add(unique );

or just turn off constraints altogether:

dataset.EnforceConstraints = false;

but this may disable some constraints that you actually want (like foreign key constraints).

Upvotes: 1

Related Questions