ERROR: duplicate key violates unique constraint "search6_idx1" - php

I'm trying to insert items into my postgres table via PHP
But get the following error message ERROR: duplicate key violates unique constraint "search6_idx1"
search6_idx1 is the index table for search6. I tried the following select setval('search6',45) and somehow that only works for sequences.

When you define an index you can optionally make it UNIQUE. Such indexes serve a double purpose:
Speed up queries
Prevent duplicates
In your case, it seems that the problem is one of these:
You are inserting values that already exist
Your index is UNIQUE by mistake
The respective solutions would be:
Don't insert dupes
Make a non-unique index

Related

How can i get the unique key violating column name at the time of execution in postgresql

I am inserting in multiple tables with foreach loop and I want unique-key column name due to which violating unique key constraints at the run time so i can modify the value and insert it.
foreach ($data as $table_name){
$sql="INSERT INTO $table_name ($column_name) VALUES ($column_vlues)"
$result=query_params($db_conn,$sql,$params)
if($result){
//Do nothing. Its fine
}else{
//If its voileting the unique key contraints then change the column value like new column value = $column_name_which_voileting.$value and then insert it.
}
}
Any help will be appriciated.
You can use pg_get_result_error() to get the PGSQL_DIAG_MESSAGE_PRIMARY, which will contain the constraint name like this:
duplicate key value violates unique constraint "uni_id_key"
From that you can extract the name of the constraint. Then you can query information_schema.constraint_column_usage to find the columns associated with that constraint (if the constraint spans multiple columns, you might have to resort to pg_catalog.pg_constraint to find their relative position).
PostgreSQL actually sends the plain constraint name along with the error message, but PHP has no way to extract that error response field.

unique key exception on multiple inserts/updates

I have application that receives multiple requests from external sources (invoices from point-of-sale units). It gets tens of requests per second, and some of those requests are the same (have same request body).
request data is transformed and saved to two associated tables (foreign key). if record already exists (queried by unique composite key), record is updated, otherwise record is added.
the problem is that sometimes if two requests with same body are received at the same time app throws exception that unique key already exists and it can't insert it:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
It's probably some kind of race condition in MySQL but can't figure it out
Whole process is wrapped in Laravel transaction. I tried setting different isolation levels. SERIALIZABLE resolves that issue but then I get lot of deadlock exceptions, and as I can see sometimes record is not saved at all.
This is simple, what happen here, you have some value declared as "UNIQUE" or "Primary Key" or something like that, and you are trying to insert again, some key restriction it's blocking the insert, this should be good, you avoid duplicate entries in your database, but what you need to do its check if the data what you are trying to insert exist before in the database, not all the columns, you should ask for your keys or key combination, I can not help you more if I don't know the data or the table...

multiple ON DUPLICATE KEY UPDATE value

This is my code:
INSERT INTO titles_production_companies (production_companies_name, production_companies_tmdb_id, title_id)
values
('United Artists', '60','1'),
('Achte Babelsberg Film', '6100','1'),
('Metro-Goldwyn-Mayer (MGM)', '8411','1'),
('Bad Hat Harry Productions', '9168','1')
ON DUPLICATE KEY UPDATE
title_id=LAST_INSERT_ID(title_id),
production_companies_name='United Artists',
production_companies_tmdb_id='60',
title_id='1',
production_companies_name='Achte Babelsberg Film',
production_companies_tmdb_id='6100',
title_id='1',
production_companies_name='Metro-Goldwyn-Mayer (MGM)',
production_companies_tmdb_id='8411',
title_id='1',
production_companies_name='Bad Hat Harry Productions',
production_companies_tmdb_id='9168', title_id='1';
and I've got this message:
Integrity constraint violation: 1062 Duplicate entry '1-Bad Hat Harry
Productions-9168' for key 'uc_production_companies''
The answer in your situation is likely more related to your (My?)SQL server than to PHP.
Let's think of the steps your code is instructed to execute:
Run a INSERT statement
Check if there is any duplicate key in the statement
If duplicate key is detected, fill some of the fields with the given (fixed) values.
Now, if you have a UNIQUE index on any of the fields you fill in by your ON DUPLICATE clause, this will most likely be duplicated on any triggered duplicate INSERT statement.
Looking at your example, it seems you have an index of UNIQUE on your
production_companies_name column which is violated on all ON DUPLICATE triggers, so only the first would work and all the rest will throw out this error.
Possible solutions:
Remove the UNIQUE type index from the production_companies_name column, if any.
Do not attempt to fill in any UNIQUE index with static values.
Do not attempt to fill in any UNIQUE index at all.
Let me know if that helps :-)

mysql adding index results to error

I am using PhpMyAdmin to add indexes on the tables but whenever i try to add one it prints errors...
I have a table called updates_categories and i have a column named created_date... so i tried to add an index there and i get the following error:
#1062 - Duplicate entry '2012-02-27 22:15:16' for key 'date_index'
i suppose that many entries have entered the same date(this can happen) but what does that mean?That i cant have an index on columns with same data?
Do not add an index for only one field becouse most of the time it has no performance benefit on select but they got disadvantages on inserts. What is your table structure and what kind of queries are you using against it?
Yes, you can - but don't use it as "UNIQUE" keys, just "simple" index.
what kind of index did you try to create?
unique and primary key require that no key exists more than once

MySQL only insert if there isn't an exact row in the Table

I need to insert this in a table but only if there isn't a replica of the row already. (both values should be equal). How can I change the code to work this way? Thanks
<?php
mysql_select_db("cyberworlddb", $con);
mysql_query("INSERT INTO Badges (UID, Website)
VALUES ('1', 'www.taringa.net')");
mysql_close($con)
?>
You could create a single index for the UID and Website columns and make that index unique, then use INSERT IGNORE. The result will be that if it is a duplicate, it will just be ignored.
If you need to be able to tell if the SQL inserted a row, then follow it up with a call to mysql_affected_rows() which should return 0 if it didn't do anything and 1 if it inserted the record.
Easiest thing to do is use INSERT IGNORE and have a unique key on the fields. It will insert if no row exists, otherwise do nothing.
What about a unique index on (UID, Website), which would cause the insert to fail?
First up, about the question. It is simple bad to check for "an exact" replica of row in RDBMS. That is just too costly. The right question to ask is what makes my row unique and what is the minimum I can get away with. Putting in unique constraints on big columns is a bad idea.
Answers saying that you should include UID in unique constraint are again just BAD. UID is most likely a generated key and the only input coming from outside is website name. So the only sane thing to do here is to put a unique constraint on website column.
Then the insert code should handle unique constraint errors coming out from the database. You can get the error number from DB handle, like
$errorNo = $mysql->errno ;
Then check for a particular code (1062 in case of MYSQL) that corresponds to unique key violation.

Categories