Creating auto increment PK via pgAdmin GUI Not Working - postgresql

I don't know what I'm missing here. I simply want an auto incremented id here. I thought setting PK to true would do that:
Then tried to run this query:
INSERT INTO public.social_type(name)
VALUES ('personal blog');
But got this error:
ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, personal blog).
SQL state: 23502

the type integer does not mean it will generate a value. To do so, select the serial type instead

Related

Is it possible to get another field of row I'm trying to duplicate in PSQL or MyBatis?

I have a table 'client', which has 3 columns - id, siebel_id, phone_number.
PhoneNumber has a unique constraint. If I save a new client with an existing number, I'll get an error ERROR: duplicate key value violates unique constraint "phone_number_unique".
Is it possible to make PSQL or MyBatis showing 'siebel_id' of a record where the phone number already saved?
I mean to get a message like
'ERROR: duplicate key value violates unique constraint "phone_number_unique"
Detail: Key (phone_number)=(+79991234567) already exists on siebel_id...'
No, it's not possible to tweak the internal message that the PostgreSQL database engine returns accompannying an error. Well... unless you recompiled the whole PostgreSQL database from scratch, and I would assume this is off the table.
However, you can easily search for the offending row using SQL, as in:
select siebel_id from client where phone_number = '+79991234567';

SERIAL works with NULL, GENERATED ALWAYS AS IDENTITY not

Postgres 12:
CREATE TABLE l_table (
id INT generated always as identity,
w_id int NOT null references w_table(id),
primary key (w_id, id)
)PARTITION BY LIST (w_id);
CREATE table l1 PARTITION OF l_table FOR VALUES IN (1);
insert into l1 (w_id) values (1);
I'm getting:
ERROR: null value in column "id" violates not-null constraint
If I replace INT generated always as identity with SERIAL it works. This is odd as in another table the generated always as identity works with null. Using default as value does not work either.
GAAI is supposed to be the SQL standard way of replacing SERIAL, even It's the suggested one. What am I missing here?
Thanks.
What am I missing here?
You're trying to insert into the partition table l1 directly, instead of the partitioned l_table. This ignores the identity column on the parent table, tries to insert the default null, and fails the non-null constraint that every identity column has. If you instead do
insert into l_table (w_id) values (1);
it will work and route the inserted row into the right partition.
Using default as value does not work either.
Apparently it's quite hard to do that. How to DEFAULT Partitioned Identity Column? over at dba.SE discusses some workarounds.

INSERT INTO excluding ID column violates primary key uniqueness constraint

I have a Postgres 10.6 table with a serial ID column.
When I attempt to insert into it:
INSERT INTO table (col1, col2) VALUES ('foo', 'bar');
excluding the ID column from the column list, I get:
ERROR: duplicate key value violates unique constraint "customer_invoice_pkey"
Detail: Key (id)=(1234) already exists.
Subsequent runs of the query increment the ID in the error message (1235, 1236 etc.)
How can this be happening?
Having a serial column does not prevent you from inserting rows with an explicit value for id. The sequence value is only a default value that is used when id is not specified in the INSERT statement.
So there must have been some “rogue” inserts of that kind. From PostgreSQL v11 on, you can use identity columns (GENERATED ALWAYS AS IDENTITY) to make overriding the sequence value harder.
You could use the setval function to set the sequence to a value higher than the maximum id in the table to work around the problem.

Issue with unique constraints in postgresql

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.

Postgresql User Defined Type in primary key constraint

I'm using postgresql 9.1 on Ubuntu 12.04. I have a user defined type as one column of a table. When I create a primary key constraint I get a syntax error.
Here is a sample sql script I'm using with psql to create the table:
CREATE TYPE my_type AS
(
field1 integer,
field2 integer
);
CREATE TABLE my_table
(
my_data my_type,
other_data integer
);
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I get this error:
ERROR: syntax error at or near "."
LINE 1: ...ble ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I've tried using (my_data).field1 but also get a syntax error.
If I just use my_data in the constraint there is no error:
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data);
But I would like to use just one field as part of the constraint.
Thanks for any ideas.
I found this question using google and I'm pretty sure is dead but I still want to answer it cause someone else might see it.
Short answer is you have to use the field's name:
ALTER TABLE "public"."carga" ADD CONSTRAINT "pk_my_table" PRIMARY KEY ("field1");