unique-constraint

ConstraintException on DataTable.Rows.Add with AutoIncrement Column

Hi, I've got some DataTables with an ID column with AutoIncrement enabled both on the MySQL server side and the ADO.NET schema side (loaded from an XML schema file). I'd like not to have to care about these IDs when inserting rows because their sole use is to have a primary key for the table – there are no foreign key references. Howe...

SQL can I have a "conditionally unique" constraint on a table?

I've had this come up a couple times in my career, and none of my local peers seems to be able to answer it. Say I have a table that has a "Description" field which is a candidate key, except that sometimes a user will stop halfway through the process. So for maybe 25% of the records this value is null, but for all that are not NULL, it ...

Special case of updating a column with not null and unique constraints in PostgreSQL

This is a toy example that illustrates a real problem in PostgreSQL. The below examples are using a PostgreSQL 8.4.3 server, but I suspect other versions have the same problem. Given the following table: => create table tmp_foo (foo boolean not null unique, bar boolean not null unique); => insert into tmp_foo (foo, bar) values (true,...

Unique Constraint vs Unique Index

I’m interested in learning which technique developers prefer to use to enforce uniqueness in SQL Server: UNIQUE CONSTRAINT or UNIQUE INDEX. Given that there is little difference in the physical implementation of each, how do you decide which is best? Are there reasons other than performance to evaluate the best solution? Are there da...

HowTo: display custom error message when unique index fails on object save

I have added an index on a table just to ensure that a set of fields together form a composite unique key (Is this approach correct? or is there a better option with Doctrine?) After having done that, when I try to save an object and the unique constraint fails, a SQL exception is generated. What is the best way to handle this? and to d...

Lost transaction with JPA unique constraint?

I have a field with an unique constraint: @Column(unique=true) private String uriTitle; When I try to save two entities with the same value, I get an exception - but another exception than I exected: java.lang.IllegalStateException: <|Exception Description: No transaction is currently active at org.eclipse.persistence.interna...

SQL: How to find duplicates based on two fields?

I have rows in an Oracle database table which should be unique for a combination of two fields but the unique constrain is not set up on the table so I need to find all rows which violate the constraint myself using SQL. Unfortunately my meager SQL skills aren't up to the task. My table has three columns which are relevant: entity_id, ...

How to specify that a combination of columns should be a unique constraint using annotations?

I want to make sure that all rows in my table have a unique combination of two fields, and I want to specify this using annotations in my entity class. I have tried using a combination of @Table and @UniqueConstraint but apparently I'm doing it wrong, in that I can only seem to specify that the separate columns should be unique (I can a...

Hibernate throws unique constraint violation exception while updating field part of unique key

Hi All, Below is the use case: I have a unique index defined on 3 columns say A,B,C. Assume the values in them are A1,B1,C1. My java code is adding a new record say A1,B1,C1 but before this record is added, i update the previous value from C1 to C2. While trying to add the new record (after the update), hibernate is throwing an unique c...

Unique constraint using data in multiple tables (SQL / SQLAlchemy)

A top class called Parametric is used to create objects which can have parameters associated with them: class Parametric(object): def __init__(self, name): self.name = name self.pars = [] class Foo(Parametric): def __init__(self, name, prop): self.prop = prop Parametric.__init__(self, name) class Bar(Parametric): def __init...

Hibernate and NonUniqueObjectException

Hi all, i have an entity that contains two other entities with @ManyToOne relationship. @Entity public class A extends Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @ManyToOne @Cascade(CascadeType.SAVE_UPDATE) private B b; @ManyToOne @Cascade(CascadeType.SAVE_UPDA...

Are unique constraints on the DB necessary?

I've been wondering lately. Lets say we're doing a webapp with JSF+Spring+JPA/Hibernate (or well any other technologies) and lets say we have a "User" entity. We want the User to have a unique login. If we want to do it then we can put a @UniqueConstraint on the "Login" column, but still our application has to check during user registrat...

Entity Framework: How to properly handle exceptions that occur due to SQL constraints

Hi all I use Entity Framework to access my SQL data. I have some constraints in the database schema and I wonder how to handle exceptions that are caused by these constraints. As example, I get the following exception in a case where two users try to add an (almost) identical entity to the DB concurrently. System.Data.UpdateException ...

Unique index or unique key?

What is the diffrence between a unique index and a unique key? ...

How can make Column that allow null to be unique from SQL Server 2005 ?

I have column in a table that allow null but i want to make constraint that make him null or unique ... How can i do that ? Note : I validate it from the frontend but i want to have a physical in sql server that allow this even developer try to enter data ...

Why is Fluent NHibernate ignoring my unique constraint on a component?

In my map I have: Component( x => x.ExposureKey, m => { m.Map(x => x.AsOfDate).Not.Nullable(); m.Map(x => x.ExposureId).Length(30).Not.Nullable(); } ).Unique(); The relevant output from the HBM is <component name="ExposureKey" insert="true" update="true" optimistic-lock="true" class="Some.Namespace.CreditE...

accepts_nested_attributes and validates_uniqueness_of

The central problem: How do you merge attribute collections by a key during mass assignment from a nested form. The details: I am using the following models: class Location < ActiveRecord::Base has_many :containers, :dependent => :destroy, :order => "container_type ASC" validates_associated :containers accepts_n...

Drop Unique Constraint on Table Column without Knowing the Constraint Name

In Oracle 10g, how can I drop a unique constraint on a column without knowing the name of the constraint (e.g. a system generated name, which won't necessarily be the same across database instances)? Dropping and recreating the table isn't an option. Is it possible? ...

Create Unqiue case-insensitive constraint on two varchar fields

In Oracle 10g, how do I add a unique case-insensitive constraint on two varchar fields? For example, given the following records already in the table: "Stephen", "Swensen" "John", "Smith" The following inserts would be invalid: "stephen", "Swensen" "John", "smith" "stephen", "swensen" But the following inserts would be valid: "St...

JPA2 unique constraint: do I really need to flush?

Hi, I have a DAO where I need to catch an unique constraint exception. To do this, the only working solution is to flush my EntityManager after the persist. Only then I come into a catch block where I have to filter out the exception. And, my DAO method needs to be wrapped in a transaction (REQUIRES_NEW) otherwise I have the RollBackExc...