unique-index

Uniqueness validation in database when validation has a condition

妖精的绣舞 提交于 2019-12-09 23:27:22
问题 Using uniqueness validations in Rails is not safe when there are multiple processes unless the constraint is also enforced on the database (in my case a PostgreSQL database, so see this blog post). In my case, the uniqueness validation is conditional: it should only be enforced if another attribute in the model becomes true. So I have class Model < ActiveRecord::Base validates_uniqueness_of :text, if: :is_published? def is_published? self.is_published end end So the model has two attributes:

Rails uniqueness constraint and matching db unique index for null column

岁酱吖の 提交于 2019-12-09 14:18:39
问题 I have the following in my migration file def self.up create_table :payment_agreements do |t| t.boolean :automatic, :default => true, :null => false t.string :payment_trigger_on_order t.references :supplier t.references :seller t.references :product t.timestamps end end I want to ensure that if a product_id is specified it is unique but I also want to allow null so I have the following in my model: validates :product_id, :uniqueness => true, :allow_nil => true Works great but I should then

Create a multicolumn index to enforce uniqueness

浪尽此生 提交于 2019-12-08 11:04:20
问题 I've got two fields in my table that if they(tat and dim) are equal I'm discarding them i.e id| tat | dim | visible 1 | 11 | 22 | true 2 | 11 | 22 | false This is considered not to be valid entry for ids 1 and 2 . Because I want to allow one dim per tat . So I have a script that cleaned these duplicates for me and after that I added index on this table to prevent this from happening like this : CREATE UNIQUE INDEX dim_tat_idx ON predictions (tat, dim); As explained quite nicely in this answer

How should I deal with my UNIQUE constraints during my data migration from Postgres9.4 to Greenplum

萝らか妹 提交于 2019-12-08 03:28:19
问题 when I execute the following sql (which is contained by a sql file generated by pg_dump of Postgres9.4) in greenplum: CREATE TABLE "public"."trm_concept" ( "pid" int8 NOT NULL, "code" varchar(100) NOT NULL, "codesystem_pid" int8, "display" varchar(400) , "index_status" int8, CONSTRAINT "trm_concept_pkey" PRIMARY KEY ("pid"), CONSTRAINT "idx_concept_cs_code" UNIQUE ("codesystem_pid", "code") ); I got this error: ERROR: Greenplum Database does not allow having both PRIMARY KEY and UNIQUE

MongoDB unique index does not work

我怕爱的太早我们不能终老 提交于 2019-12-07 14:39:14
问题 I have a portion of simple code, that has to fail because of unique index constraint. But both of the objects are added to database and can be queried, in spite of unique index. BasicDBObject typeUrlIndex = new BasicDBObject(); typeUrlIndex.put(FIELD_TYPE_URL, 1); BasicDBObject typeUrlIndexOptions = new BasicDBObject(); typeUrlIndexOptions.put("background", true); typeUrlIndexOptions.put("sparse", true); typeUrlIndexOptions.put("unique", true); typeUrlIndexOptions.put("dropDups", true);

Conditional Unique index on h2 database

我们两清 提交于 2019-12-07 09:21:14
问题 I have a SAMPLE_TABLE with a column BIZ_ID which should be unique when the column active is not equal to 0. On an oracle database the index looks like this: CREATE UNIQUE INDEX ACTIVE_ONLY_IDX ON SAMPLE_TABLE (CASE "ACTIVE" WHEN 0 THEN NULL ELSE "BIZ_ID" END ); How would this unique index look like on a h2 database? 回答1: In H2, you could use a computed column that has a unique index: create table test( biz_id int, active int, biz_id_active int as (case active when 0 then null else biz_id end)

Uniqueness validation in database when validation has a condition on another table

纵然是瞬间 提交于 2019-12-07 08:47:24
问题 I asked a similar question here at Uniqueness validation in database when validation has a condition but my requirements have changed, hence this question. Using uniqueness validations in Rails is not safe when there are multiple processes unless the constraint is also enforced on the database (in my case a PostgreSQL database so see http://robots.thoughtbot.com/the-perils-of-uniqueness-validations). In my case, the uniqueness validation is conditional: it should only be enforced if another

MySQL Views in Navicat - How to define 'primary key'?

我们两清 提交于 2019-12-07 05:19:28
问题 Often when I define a View in Navicat I receive the following message: xxx does not have a primary key. Updates to this table will be done using the following pseudo statement: UPDATE xxx SET ModifiedFieldsAndValues WHERE AllFieldsAndOldValues LIMIT 1 Obviously I only use my Views for viewing data, not updating . But this did make me curious: Is there a way to define a "primary key" or "unique index" on a View? 回答1: its implied that the view uses the indices and primary keys of its base table

MyISAM unique keys being cut off at 64 bytes, causing collisions

老子叫甜甜 提交于 2019-12-07 05:11:08
问题 I've got a MySQL table that stores urls as unique keys. I'm starting to get collisions on my keys because it seems the keys themselves are only the first 64 bytes (or characters if you prefer, its a latin-1 collated) of any url. So if a url is over 64 characters and I've already got a similar url it throws an error. For example: SELECT l.link_id FROM mydb.links l WHERE url = 'http://etonline.com/tv/108475_Charlie_Sheen_The_People_Have_Elected_Me_as_Their_Leader/index.html' Throws this error:

How should I deal with my UNIQUE constraints during my data migration from Postgres9.4 to Greenplum

删除回忆录丶 提交于 2019-12-06 16:49:07
when I execute the following sql (which is contained by a sql file generated by pg_dump of Postgres9.4) in greenplum: CREATE TABLE "public"."trm_concept" ( "pid" int8 NOT NULL, "code" varchar(100) NOT NULL, "codesystem_pid" int8, "display" varchar(400) , "index_status" int8, CONSTRAINT "trm_concept_pkey" PRIMARY KEY ("pid"), CONSTRAINT "idx_concept_cs_code" UNIQUE ("codesystem_pid", "code") ); I got this error: ERROR: Greenplum Database does not allow having both PRIMARY KEY and UNIQUE constraints why greenplum doesn't allow this? I really need this unique constraint to guarantee some rule,