In postgres I have created a table by name twitter_tweets. In this table I have assigned constraint for tweet_text column by using the command
ALTER TABLE ONLY twitter_tweets
ADD CONSTRAINT twitter_tweets_pkey PRIMARY KEY (tweet_text);
The constraint has applied by getting message i.e., alter table
but while parsing the data it showing runtime exception i.e.,
java.lang.RuntimeException: Failed to execute insert query insert into twitter_tweets (tweet_created_at, tweet_id, tweet_id_str, tweet_text, tweet_source, tweet_truncated, tweet_in_reply_to_status_id, tweet_in_reply_to_status_id_str, tweet_in_reply_to_user_id, tweet_in_reply_to_user_id_str, tweet_in_reply_to_screen_name, tweet_geo,tweet_coordinates, tweet_at_reply, tweet_is_quote_status, tweet_retweet_count, tweet_favorite_count, tweet_favorited, tweet_retweeted, tweet_lang, tweet_possibly_sensitive, tweet_filter_level, tweet_scopes_S)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) at Demo.JdbcClient.executeInsertQuery(JdbcClient.java:62) at Demo.PsqlBolt.execute(PsqlBolt.java:91) at backtype.storm.daemon.executor$fn__5694$tuple_action_fn__5696.invoke(executor.clj:690) at backtype.storm.daemon.executor$mk_task_receiver$fn__5615.invoke(executor.clj:436) at backtype.storm.disruptor$clojure_handler$reify__5189.onEvent(disruptor.clj:58) at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:132) at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:106) at backtype.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:80) at backtype.storm.daemon.executor$fn__5694$fn__5707$fn__5758.invoke(executor.clj:819) at backtype.storm.util$async_loop$fn__545.invoke(util.clj:479) at clojure.lang.AFn.run(AFn.java:22) at java.lang.Thread.run(Thread.java:745) Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "twitter_tweets_pkey" Detail: Key (tweet_text)=() already exists. at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:405) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2892) at com.zaxxer.hikari.proxy.StatementProxy.executeBatch(StatementProxy.java:116) at com.zaxxer.hikari.proxy.PreparedStatementJavassistProxy.executeBatch(PreparedStatementJavassistProxy.java) at Demo.JdbcClient.executeInsertQuery(JdbcClient.java:50) ... 11 more
The below image1 is the table to which i have used constraint
This is my output after keeping constraints
Your problem is described here:
ERROR: duplicate key value violates unique constraint "twitter_tweets_pkey" Detail: Key (tweet_text)=() already exists. at
You set tweet_text to be your PRIMARY KEY (PK), and as PK it cant get duplicated data.
At some point you already insert the data that you are trying to insert now into this column (tweet_text).
Now, why not create an Integer column, AUTO INCREMENTED, something like ID? The way as it now, you are telling me that no one should post a same text that was posted by other user.
Ex. If User A post a tweet with content (tweet_text) : "Hello World", no other user can post the same content.
Unique Constraint Violation
You asked for a primary key. A primary key in Postgres automatically creates an index and a UNIQUE constraint.
Then you inserted rows of data. At least two of those rows had the same value in that primary key field. The duplicate data violated the UNIQUE constraint. Postgres then did its duty in refusing to store the offending data. That refusal is reported back to you, the Java programmer, as an Exception.
At least that is my guess based on this excerpt from the middle of your error text:
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "twitter_tweets_pkey" Detail: Key (tweet_text)=() already exists.
How do I get details for a given constraint name in PostgreSQL? I have the following error message, and I am trying to determine which table and columns the constraint is referring to:
duplicate key value violates unique constraint \"events_pkey\"\n Detail: Key (id)=(258) already exists."
It says in the message that the primary key column id uniqueness is violated. You are trying to insert the row with id=258, but it already exists.
I keep getting this error within my application
duplicate key value violates unique constraint "product_supplierinfo_pkey"
DETAIL: Key (id)=(409) already exists.
This is on table product_supplierinfo.
The actual next sequence number the key constraint needs to be is 5461 not 409.
Can someone please tell me the correct query to update this key unique constraint?
#chris Collins, please post the output of \d product_supplierinfo. I imagine you created this table with id serial.
You should see the name of the sequence from which the next default value for the id field will come. It will probably be product_supplierinfo_id_seq.
Then do, assuming the above names are correct, SELECT * from product_supplierinfo_id_seq;. You will probably see that the next value is 410.
If this is all correct, do SELECT setval('product_supplierinfo_id_seq', 5461);.
I am following an PostgreSQL book, and had to import a CSV file into a table census.lu_tracts.
Problem: When performing the INSERT query as shown below, I get the error:
ERROR: duplicate key value violates unique constraint "pk_lu_tracts"
DETAIL: Key (tract_id)=(25001010800) already exists.
How did the key becomes duplicate? SELECT * from lu_tracs shows 0 rows.
CREATE SCHEMA census;
set search_path=census;
CREATE TABLE lu_tracts(tract_id varchar(11), tract_long_id varchar(25)
, tract_name varchar(150)
, CONSTRAINT pk_lu_tracts PRIMARY KEY (tract_id));
INSERT INTO lu_tracts( tract_id, tract_long_id, tract_name)
SELECT geo_id2, geo_id, geo_display
FROM staging.factfinder_import
WHERE geo_id2 ~ '^[0-9]+';
The right answer is DISTINCT ON (geo_id2) which will select only one row per geo_id2 (more in the manual), it should be accompanied by an ORDER BY clause that will specify what row will be choosen.
I have a table "tbl_project_user_assignment" with a compound primary key.
It is made up of project_id and user_id
Each of these are also a foreign key to the project and user tables respectively.
At the moment, I have 2 entries in this table as below...
project_id | user_id
--------------------
1 | 1
1 | 2
When I run this sql query...
INSERT INTO
tbl_project_user_assignment
(project_id, user_id) VALUES (2, 1);
...I get the following error message:
Integrity constraint violation: 1062
Duplicate entry '1' for key
'FK_project_user'
The FK_project_user key is the one linking the project_id to the tbl_project id.
This doesn't make sense to me because the values I'm inserting are unique...
Any ideas?
It looks like FK_project_user is a unique key. Try dropping temporarily that constraint and perform the insert again.
If insert works, recreate the constraint making sure it's not flagged as unique anymore.
I think you should have:
a foreign key relation to project
a foreign key relation to user
a unique constraint on (project, user)
possibly a primary key on the combination of (project, user) OR a separate key field
If you choose a combined PK you wouldn't need an extra constraint of course.
And it seems you have a unique constraint on user (by itself).
Posting the show create table statement would help even more.