unique-constraint

Does Entity Framework 5 support unique constraints?

走远了吗. 提交于 2019-11-28 09:43:40
Wondering if Entity Framework 5 supports unique constraints on entity properties? If so, how can I specify that a property should be unique? No, it doesn't. There were plans in the past to include a unique constraint feature in EF 5.0: http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx But you can see that there is an update on top of the post: Update: this feature has been postponed and will not be included in Entity Framework 5. You can vote on the feature to raise possibly the priority it gets implemented with... http://data.uservoice.com

Using unique constraint on Hibernate JPA2

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:40:28
How can I implement my unique constraints on the hibernate POJO's? assuming the database doesn't contain any. I have seen the unique attribute in @Column() annotation but I couldn't get it to work? What if I want to apply this constraint to more than one column? Bascially, you cannot implement unique constraint without database support. @UniqueConstraint and unique attribute of @Column are instructions for schema generation tool to generate the corresponsing constraints, they don't implement constraints itself. You can do some kind of manual checking before inserting new entities, but in this

How to drop a unique constraint from table column?

大憨熊 提交于 2019-11-28 09:02:33
I have a table 'users' with 'login' column defined as: [login] VARCHAR(50) UNIQUE NOT NULL Now I want to remove this unique constraint/index using SQL script. I found its name UQ_ users _7D78A4E7 in my local database but I suppose it has a different name on another database. What is the best way to drop this unique constraint? Or at least any... Thanks. SKINDER, your code does not use column name. Correct script is: declare @table_name nvarchar(256) declare @col_name nvarchar(256) declare @Command nvarchar(1000) set @table_name = N'users' set @col_name = N'login' select @Command = 'ALTER TABLE

Hibernate and NonUniqueObjectException

时光毁灭记忆、已成空白 提交于 2019-11-28 08:17:43
问题 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_UPDATE) private C c; } If i try to save an A instance that have "B_ID" and "C_ID" of another A record i get the exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier

How to annotate unique constraint with WHERE clause in JPA

依然范特西╮ 提交于 2019-11-28 07:42:48
问题 I need to use these unique constraints in PostgreSQL CREATE UNIQUE INDEX favorites_3col_uni_idx ON favorites (user_id, menu_id, recipe_id) WHERE menu_id IS NOT NULL; CREATE UNIQUE INDEX favorites_2col_uni_idx ON favorites (user_id, recipe_id) WHERE menu_id IS NULL; The first one I annotate in JPA: @Table(uniqueConstraints= { @UniqueConstraint(name="favorites_3col_uni_idx", columnNames = {"user_id", "menu_id", "recipe_id"}) }) But, ¿it is possible to annotate in JPA the second unique index?

SQL: How to find duplicates based on two fields?

三世轮回 提交于 2019-11-28 07:28:38
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, station_id, and obs_year. For each row the combination of station_id and obs_year should be unique, and I want to find out if there are rows which violate this by flushing them out with an SQL query. I have tried the following SQL (suggested by this previous

Django - save() update on duplicate key

﹥>﹥吖頭↗ 提交于 2019-11-28 06:53:17
I have little application which allows a user to rate a video. The user can rate only once. So I have defined the uniqueness on the model. But he should be able change his rate. So the save() should update on duplicate key class VideoRate(models.Model): """Users can Rate each Video on the criterias defined for the topic""" user = models.ForeignKey(User) video = models.ForeignKey(VideoFile) crit = models.ForeignKey(VideoCrit) rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES) class Meta: unique_together = (('user', 'video', 'crit'),) verbose_name = 'Video Rating'

How can I create a SQL unique constraint based on 2 columns?

霸气de小男生 提交于 2019-11-28 04:28:04
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- | 12456 | Ax759 | Joe Smith | 12456 | Ax760 | Mary Smith | 12458 | Ax739 | Carl Lewis | 12460 | Ax759 | Chuck Norris | 12460 | Bx759 | Bruce Lee I need to add a constraint to this table so that no user can have duplicate contact id's. The users are importing data from various external systems so ContactId will not be unique across the board but will be unique on a per user basis. I know how to create Unique and Non-Null contraints based on single columns but how can I create a unique

Postgresql: Conditionally unique constraint

有些话、适合烂在心里 提交于 2019-11-28 03:52:57
I'd like to add a constraint which enforces uniqueness on a column only in a portion of a table. ALTER TABLE stop ADD CONSTRAINT myc UNIQUE (col_a) WHERE (col_b is null); The WHERE part above is wishful thinking. Any way of doing this? Or should I go back to the relational drawing board? PostgreSQL doesn't define a partial (ie conditional) UNIQUE constraint - however, you can create a partial unique index . PostgreSQL uses unique indexes to implement unique constraints, so the effect is the same, you just won't see the constaint listed in information_schema . CREATE UNIQUE INDEX stop_myc ON

Quickest way to check for pre-existing record before insert [mysql_errno()]

青春壹個敷衍的年華 提交于 2019-11-27 22:52:51
问题 My question will use emails as an example, but this could apply to anything. Normally before registering a new user (including inserting his/her email) I check if his/her email already exists in the DB something like this: $result = mysql_query("SELECT * FROM Users WHERE email = '".mysql_real_escape_string($email)"';"); if(!$result) { die(mysql_error()); } if(mysql_num_rows($result) > 0) { die('<p>Email already in DB. <a....>Recover Email</a></p>'); } else { //insert new user data into Users