2016-01-23 217 views
-3

在postgres中,我通过名称twitter_tweets创建了一个表。在该表中我以postgresql中的唯一约束问题

ALTER TABLE ONLY twitter_tweets 
ADD CONSTRAINT twitter_tweets_pkey PRIMARY KEY (tweet_text); 

约束已通过获取消息即所施加的命令分配给tweet_text列约束,改变表

但在解析数据它表示运行时异常即

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

下面是此搜索到我已经使用约束表twitter_tweets table

这是经过我的输出保持约束I got only one field data but not all the fields

+0

好像你要发送一个空tweet_text –

+0

我不发送该字段为空@FabrizioMazzoni –

+0

的作为张贴在您发送重复值的答案中。而是改变表格以包含适当的主键。 –

回答

1

您的问题说明如下:

ERROR: duplicate key value violates unique constraint "twitter_tweets_pkey" Detail: Key (tweet_text)=() already exists. at

您设置tweet_text是主键(PK),和PK它不能得到重复的数据。

在某些时候,您已经将您尝试插入的数据立即插入此列(tweet_text)。

现在,为什么不创建一个整数列,AUTO INCREMENTED,像ID一样?就像现在这样,你告诉我,没有人应该发布其他用户发布的相同文本。

Ex。如果用户A发布带有内容的推文(tweet_text):“Hello World”,则其他用户不能发布相同的内容。

+0

可以发布相同的内容..但我没有获取所有表格数据。我只有一个领域。在没有应用约束之前,我得到了所有的表数据,但是在添加约束之后,当您使用此约束(PK)时,它会抛出运行时异常@ psantos –

+0

@JayaSreeMeruga,具有此约束的列将不会接受重复数据。出于这个原因,我建议用这个约束创建一个像'ID'这样的新列。 – psantos

0

唯一约束违规

您要求输入主键。 Postgres中的主键自动创建一个索引和一个UNIQUE约束。

然后插入数据行。这些行中至少有两个在主键字段中具有相同的值。重复数据违反了UNIQUE约束。 Postgres随后拒绝存储违规数据。作为一个例外,这个拒绝被报告给你,Java程序员。至少这是我的猜测基于此摘录从你的错误文本中间

Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "twitter_tweets_pkey" Detail: Key (tweet_text)=() already exists.

+0

是的,那是我的主要例外@Basil Bourque –

+0

@JayaSreeMeruga如果你明白,你的问题到底是什么? –

+0

为什么我得到那个错误?为什么它表明它已经存在?你能否详细地告诉我关于解决方案的问题,因为我不明白你在答案中发布了什么 –