Rails: Oracle constraint violation Rails: Oracle constraint violation oracle oracle

Rails: Oracle constraint violation


Based on the name of the constraint, PK_REGISTRATION_OWNERSHIP, you have a primary key violation. If these databases aren't maintaining this data in lockstep, something/someone has already inserted a record into the registration_ownerships table in your production database with company_ownership_id=2 & registration_id=2920. (I'm guessing at the specifics based on the names)

If this particular set of values needs to exist in the production database,

1) check that what's already there isn't what you're trying to insert. if it is, you're done.

2) If you need to insert your sample data as-is, you need to modify the existing data & re-insert it (and all the dependent/refering records), then you can insert your values.


If you query the table and find no matching rows, then one of the following may be the cause:

  1. The session is trying to insert the row twice.
  2. Another session has inserted the row, but hasn't committed yet.

Also, check that the state of the unique constraint is the same between dev and prod. Perhaps the one on dev is marked as not validated - check that the index exists on dev and is a unique index (note: in Oracle it is possible to have a unique constraint validated by a non-unique index).


Take a hard look at the underlying unique index for the constraint. The reason dropping the constraint doesn't change anything is because the index remains, and it's a unique index. What does the following tell you about the indexes in both environments? Are both indexes valid? Are both defined the same? Are they both actually unique?

SELECT ai.table_name, ai.index_name, ai.uniqueness, aic.column_name, ai.status  FROM all_constraints ac JOIN all_indexes ai ON (ac.index_name = ai.index_name)                          JOIN all_ind_columns aic ON (ai.index_name = aic.index_name) WHERE ac.owner = 'YOUR_USER'   AND ac.constraint_name = 'PK_REGISTRATION_OWNERSHIP' ORDER BY ai.index_name, column_position;