Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:54:53+00:00 2026-06-11T07:54:53+00:00

Ive got a question regarding the validation of uniqueness. From : http://guides.rubyonrails.org/active_record_validations_callbacks.html#uniqueness It does

  • 0

Ive got a question regarding the validation of uniqueness.

From : http://guides.rubyonrails.org/active_record_validations_callbacks.html#uniqueness

“It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index in your database.”

Does that mean whenever I validate uniqueness I have to add an index in the database? Or is this only necessary when its likely to happen that two recors are going to be inserted at the same time?

Whats the best practice here: Unique and Index always together or dependant on the situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:54:55+00:00Added an answer on June 11, 2026 at 7:54 am

    It is essentially a race condition. In that Alice signs up with the email address alice@gmail.com and hits the submit button twice. Because she hit them so quickly, she’s only has them in memory, and they pass validation. Then both are written to the database. The solution to this is to require the database to check for uniqueness as well between the time they are stored in memory or written to the database.

    What you need to do is add a line like this to the migration:

    add_index :table_name, :column_name, unique: true
    

    As far as best practices, you for sure want to do it when having two of the same would cause issues. It’s probably not a huge deal if two tweets are submitted with the same content, but if you have two users with the same email address, that becomes a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and normal filters defined
I've got a question regarding sharedApplication. I'm reading a particular link from my XML
I've got a question regarding jQuery, I want to remove the following from the
I've got a question regarding a SQL-select-query: The table contains several columns, one of
I've got another question regarding to basic Android programming: How can I access the
I've got a question regarding the Telerik RadGrid control client side binding. I want
I am currently reading Dive into HTML5 and I've got a question regarding the
I've got a pretty straight forward question regarding viewing text in Firefox. My application
I've got a probably embarrassingly easy question regarding Hibernate: When I've got a persisted
I've got a question regarding git branching protocol. I'm starting to get more familiar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.