unique-constraint

Is there a good way to do this in SQL?

你。 提交于 2019-12-06 05:20:42
问题 I am trying to solve the following problem entirely in SQL (ANSI or TSQL, in Sybase ASE 12), without relying on cursors or loop-based row-by-row processing. NOTE: I already created a solution that accomplishes the same goal in application layer (therefore please refrain from "answering" with "don't do this in SQL"), but as a matter of principle (and hopefully improved performance) I would like to know if there is an efficient (e.g. no cursors) pure SQL solution. Setup : I have a table T with

Save multiple objects with a unique attribute using Django formset

荒凉一梦 提交于 2019-12-06 03:25:55
TL;DR: How do you save/validate multiple objects with a unique attribute using a formset? Let's say I have a Machine which has multiple Setting s (see models below). These settings should have a unique ordering within the machine. This can be accomplished by defining a unique_together attribute in the Meta class of the Setting model: unique_together = (('order', 'machine'), ) However, I'm using an inline formset to update all the Setting s of a Machine . Assume I have the following info in my formset: Setting 1: machine=1; order=1 Setting 2: machine=1; order=2 If I were to switch the order on

Neo4J 2.1.3 Uniqueness Constraint Being Violated, Is This A Bug?

回眸只為那壹抹淺笑 提交于 2019-12-06 02:53:44
问题 We have a Neo4J 2.1.3 database and we have a uniqueness constraint that was created as follows: CREATE CONSTRAINT ON (segment:SEGMENT) ASSERT segment.segmentId IS UNIQUE When we test this from the browser, it works fine in detecting violations, e.g: CREATE (n:SEGMENT {name : "duplicate", segmentId : 110484}) RETURN n results in Node 589 already exists with label SEGMENT and property "segmentId"=[110484] Neo.ClientError.Schema.ConstraintViolation which is fine. We have a load tester setup with

SQL constraint to check whether value doesn't exist in another table

最后都变了- 提交于 2019-12-06 02:34:51
In my PostgreSQL 9.4 database, I have a table fields with a column name with unique values. I'm creating a new table fields_new with a similar structure (not important here) and a column name as well. I need a way to constraint name values to be inserted to the fields_new not to be present in fields.name . For example, if fields.name contains the values 'color' and 'length' , I need to prevent fields_new.name from containing 'color' or 'length' values. So, in other words I need to provide that the name columns in both tables do not have any duplicate values between them. And the constraint

Unique constraint on two columns regardless of order

末鹿安然 提交于 2019-12-05 18:22:06
I have the following table definition: CREATE TABLE [Car] ( CarID int NOT NULL PRIMARY KEY IDENTITY(1,1), FirstColorID int FOREIGN KEY REFERENCES Colors(ColorID), SecondColorID int FOREIGN KEY REFERENCES Colors(ColorID), UNIQUE(FirstColorID, SecondColorID) ) I want the two Color columns to be unique, regardless of the combination they appear in. E.g. attemping: INSERT INTO Car (FirstColorID, SecondColorID) VALUES (1, 2); --should succeed but then trying the same after that first record exists with the colors reversed should fail: INSERT INTO Car (FirstColorID, SecondColorID) VALUES (2, 1); -

How to create a Postgres table with unique combined primary key?

女生的网名这么多〃 提交于 2019-12-05 18:01:28
I have two tables named players & matches in a Postgres DB as follows: CREATE TABLE players ( name text NOT NULL, id serial PRIMARY KEY ); CREATE TABLE matches ( winner int REFERENCES players (id), loser int REFERENCES players (id), -- to prevent rematch btw players CONSTRAINT unique_matches PRIMARY KEY (winner, loser) ); How can I ensure that only a unique combination of either (winner, loser) or (loser, winner) is used for matches primary key so that the matches table won't allow the insertion of: INSERT INTO matches VALUES (2, 1); If it already has a row containing VALUES (1, 2) like :

ORM UniqueConstraint, null value duplicate

你离开我真会死。 提交于 2019-12-05 15:02:40
I have an entity with an unique constraint on 3 fiels. 2 fields are not nullable, and the third is nullable. My problem, when the third field is null, the unique constraint doesn't work, so i can have in my database some duplicate values like ( 5,1,null)(5,1,null) For me, ( 5,1,null) is an unique value, with null too. Do you have any ideas ? This is my ORM line : * @ORM\Table(name="table", uniqueConstraints={@ORM\UniqueConstraint(name="table_idx", columns={"field1", "field2", "field3"})}) Edit : The nullable value is a foreign key to an other entity, so i can't put a custom value. It's an

Hibernate - clearing a collection with all-delete-orphan and then adding to it causes ConstraintViolationException

﹥>﹥吖頭↗ 提交于 2019-12-05 13:34:53
问题 I have these entities class Foo{ Set<Bar> bars; } class Bar{ Foo parent; String localIdentifier; } With this mapping (sorry, no annotations, I'm old fashioned): <class name="Foo"> ... <set name="bars" cascade="all-delete-orphan" lazy="false" inverse="true"> <key>...</key> <one-to-many class="Bar"/> </set> </class> <class name="Bar"> ... <property name="localIdentifier" column="local_identifier"/> <many-to-one name="parent" column="parent_id" /> </class> I also have a unique constraint on 2

Should I specify both INDEX and UNIQUE INDEX?

时光怂恿深爱的人放手 提交于 2019-12-05 11:02:58
问题 On one of my PostgreSQL tables, I have a set of two fields that will be defined as being unique in the table, but will also both be used together when selecting data. Given this, do I only need to define a UNIQUE INDEX, or should I specify an INDEX in addition to the UNIQUE INDEX? This? CREATE UNIQUE INDEX mytable_col1_col2_idx ON mytable (col1, col2); Or this? CREATE UNIQUE INDEX mytable_col1_col2_uidx ON mytable (col1, col2); CREATE INDEX mytable_col1_col2_idx ON mytable (col1, col2); 回答1:

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

自古美人都是妖i 提交于 2019-12-05 10:11:55
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.CreditExposureKey, Some.Namespace, Version=0.0.0.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa"> <property name="AsOfDate" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="AsOfDate" not-null="true"/> </property> <property name=