Codeigniter ORA-00001: unique constraint - oracle

This error: Message: oci_execute (): ORA-00001: unique constraint (SCHEMA_NAME.NAME CONSTRAINT) violated
I wonder if I do not how to handle the error more simply, more generic.
Because otherwise I will have to work on each function of the models to check the data before adding, for no duplications and not give the error noted above.
Does anyone know a simple way to solve this?
Thanks.

There is a hint that can be specified that will allow the statement to succeed without inserting the duplicated data. It can be useful for replication or bulk data loading where the job may attempt to insert the same data multiple times. I wouldn't recommend it as part of a user application.
"The IGNORE_ROW_ON_DUPKEY_INDEX hint applies only to single-table INSERT operations. It is not supported for UPDATE, DELETE, MERGE, or multitable insert operations. IGNORE_ROW_ON_DUPKEY_INDEX causes the statement to ignore a unique key violation for a specified set of columns or for a specified index. When a unique key violation is encountered, a row-level rollback occurs and execution resumes with the next input row."

Related

Log Postgresql sequence value that is missing in main table

Gaps on id column occurs due to the usage of sequences on PostgreSQL as the default value of the table’s primary key. When the transaction rollback the sequence value will not rollback thus the id column will be not sequential. I have tried to eliminate the gaps but it seems to impact the performance. What I am thinking right now is to log those missing IDs along with error message and request payload. As a result, I am struggling getting the ID that was generated by sequence when DB transaction rollback.
Please suggest approach to get the missing sequence ID from DB when function is rollbacked so we can log it.

How to recognize what constraint cause an exception in an insertion in Sqlite

How could I know what type of constraint exception is raised in an insertion in Sqlite. What it caused?. For example if I design a table with an unique constraint and primary key constraint, and I insert a row that violate a constraint, sqlite will raise an SqliteConstraintException, but I would like to know which of both constraints was violated. That would be helpful for creating an error message for the user. Instead of writing functions that validate the row before inserting it. Thank you for your help.
That information is not provided as a field on the Exception. You can use the getMessage() method to get some information about the cause. Like UNIQUE constraint failed: event.id as an example. This is telling you the name and the column which violated the constraint, but not the name of the constraint which is violated.

Unique Constraint Violated on empty table

I recently received a case which my client came across the ORA-00001: unique constraint violated error. This happened when a program tried to truncate two tables and then insert data into them.
From the error-log file, the truncate step was completed,
delete from INTERNET_GROUP
delete from INTERNET_ITEM
BUT right after this, the insertion to the Internet_group table triggered the ORA-00001 error. I am wondering if there is any database settings related to this error? I never used Oracle and am wondering if Oracle puts a lock on a row with SELECT statement, in which case the row is locked and not deleted somehow? Any help is appreciated.
Please know that there is a difference between truncate and delete. You say you truncated the table, but you mention "delete from" . That is entirely different.
If you're sure you want to empty the tables, try replacing with
truncate table internet_group reuse storage;
Mind you that a commit is not necessary with the truncate statement as this is considered a DDL (data definition language) statement and not a DML (Data modification language) statement like updates and deletes.
Also, there is no row locking on selects. But changes are only applied and visible for other sessions in the database when commit-ed.
I guess that is wat happened; you deleted the records but did not execute a commit (yet) and subsequently inserted new records.
edit:
I now realize you're probably inserting multiple records....
The other option might be, that the data itself causes a violation. Can you please provide the constraints on the table? There must be a primary key or unique constraint. You might want to hold that against your dataset.

How to set the value for table sequence in db unit?

While running the DbUunit test cases I am getting following exception:
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (EMP_SYS.PK_EMP_ID) violated.
After analyzing I understood that DatabaseOperation CLEAN_INSERT will just clean the data from the table but not reset the sequence.
Can anyone help me out to find any way to reset the SEQUENCE value through DB unit?
Thanks in advance...!!
In your dataset.xml file you must specify tables in correctly insertion order, this means, the basic tables first and then the related tables. This way DatabaseOperation.CLEAN_INSERT will delete tables in correctly delete order too (related tables first and then basic tables).
Hope this helps.

In Postgresql, how do I debug an error that happens after a delete cascades?

I'm deleting this row from a table and it has a bunch of cascades on the FKs and eventually it gives me this error:
ERROR: insert or update on table "foo_route" violates foreign key constraint "foo_route_bar_fk"
SQL state: 23503
Detail: Key (bar_key)=(2176) is not present in table "bar".
foo_route_bar_fk is defined like this:
ALTER TABLE foo_route
ADD CONSTRAINT foo_route_bar_fk FOREIGN KEY (bar_key) REFERENCES bar (bar_key)
MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE;
I'm getting this error by deleting a table unrelated to both of these. I think what's happening is a trigger or a cascade is causing this error, but it's difficult to find out why.
My question is, how do you debug issues like this in postgresql? What are the series of steps that caused this error? Postgresql only tells me the result of the last thing it did before it failed. If this were code the error would give an extremely helpful stack trace. Is there a way to see something like that in Postgresql?
Typically your postgresql log will include the statement which triggered the error. That should allow you to follow the chains of cascading events.
One thing I would suggest however is that you might want to draw out a map of fkeys and look at the mappings of ON events on foreign keys. If it were me I would start with a schema only dump or pg_autodoc output, and go from there. The obvious problem is that you have deletes cascading in cases where it can't so you need to take a look and rethink things here.

Resources