Rails uniqueness constraint and matching db unique index for null column?

Validates_uniqueness_of:product_id, :if => lambda {!self. Product_id. Nil?

}.

It depends on your db server. As for mysql: A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row.

This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.

If you are going to have NULL values for a unique column, its best to handle this at the Application level Use validates_uniqueness_of validation callback.

Some major database systems do not allow a unique index to contain multiple NULLs: unique applies to NULLs as well as non-NULLs. There are ways around this on the database level (e.g. , triggers, or a computed column; see link text). You could address this on an application level and put in a validation that checks for uniqueness if the product_id is not null.

Validate :enforce_unique_product_id def enforce_unique_product_id if (!self. Product_id. Nil?

&& PaymentAgreement. Exists?(:conditions=>'product_id =?', self. Product_id)) errors.

Add_to_base('There is already an agreement with product id " + self. Product_id) end end (Update: As pointed out by zed_0xff, MySql allows multiple NULLs in a UNIQUE index in the most commonly used storage engines. ).

Cannot insert duplicate key row in object 'dbo. Test' with unique index 'UniqueIndex'. The duplicate key value is (sample ).

The statement has been terminated. I assume the index cannot store trailing spaces. Can anyone point me to official documentation that specifies/defines this behavior?

Is there a setting to change this behavior, that is, make it recognize 'sample' and 'sample ' as two different values (which they are, by the way) so both can be in the index. Why on Earth is the SELECT returning a row? SQL Server must be doing something really funny/clever with the spaces in the WHERE clause because if I remove the uniqueness in the index, both INSERTs will run OK and the SELECT will return two rows!

Any help/pointer in the right direction would be appreciated.

It depends on your db server. As for mysql.

If you are going to have NULL values for a unique column, its best to handle this at the Application level.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions