Ruby on Rails Monday, January 31, 2011

Karl Smith wrote in post #978748:
> Hmm, so you are saying I could use a multi-field index (:user_id,
> :credit_card_number). Then, during inserts if a duplicate
> :credit_card_number is attempted a ActiveRecord::StatementInvalid would
> be
> generated, thus giving me an indication to try another
> credit_card_number.
> Sounds workable, but is it realistic?

Yes, this is really common in database design. There was a time when
composite primary keys were common and this is how those were enforced
to be unique. However, the use of the technique is not limited to
composite keys.

> I don't like that, but nice to know.

Some databases use sequences exclusively for generating primary keys. As
opposed to providing auto-incrmenting column types... Just FYI.

>> My concern about locking the entire table would be that if something
>> went wrong then you might end up in a state where your entire table is
>> stuck in a locked state. I don't know for sure if that's an issue with
>> PostgreSQL, but something to consider.
> Which concerns me as well.
> I like that idea. That way I'm only locking that sequence table. This
> way I
> could use row level (pessimistic) locking.
> So, have you are anyone else tried this?

Yes, I have used this technique for generating serial sequences on a
number of projects. And you are correct that row level locking is used
to manage concurrent access.

Posted via

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment