Table in Postgrethe ID field is not auto incremented

Asked

Viewed 1,039 times

0

I’ll explain my problem:

In my SQL system already came the error in INSERT, stating that the id field, which is autoincrement is stating that it is null:

INSERT INTO cliente_sub_grupo (id,id_cliente,subgrupo,tipo_atendimento,ind_valor_titular_igual_contrato,ind_valor_dependente_igual_contrato,valor_titular,valor_dependente)  VALUES (:id,:id_cliente,:subgrupo,:tipo_atendimento,:ind_valor_titular_igual_contrato,:ind_valor_dependente_igual_contrato,:valor_titular,:valor_dependente)
INSERT INTO cliente_sub_grupo (id,id_cliente,subgrupo,tipo_atendimento,ind_valor_titular_igual_contrato,ind_valor_dependente_igual_contrato,valor_titular,valor_dependente)  VALUES (:id,:id_cliente,:subgrupo,:tipo_atendimento,:ind_valor_titular_igual_contrato,:ind_valor_dependente_igual_contrato,:valor_titular,:valor_dependente)

org.postgresql.util.Psqlexception: ERROR: null value in column "id" violates not-null Constraint Detail: Failing Row contains (null, 12, vcxvxc, F, t, t, 1, 1, active).

After looking at the code I decided to look directly in the BD and in the following image I do a test registration, leaving the ID field as blank and gives the error of the second image:

inserir a descrição da imagem aqui

This is the error when I insert directly from BD (the same error in my application console in INSERT):

inserir a descrição da imagem aqui

The same console error:

client-sub-group.Insert org.postgresql.util.Psqlexception: ERROR: null value in column "id" violates not-null Constraint Detail: Failing Row contains (null, 12, vcxvxc, F, t, t, 1, 1, active).

org.postgresql.util.Psqlexception: ERROR: null value in column "id" violates not-null Constraint Detail: Failing Row contains (null, 12, vcxvxc, F, t, t, 1, 1, active).

What can be and how to fix this mistake.

2 answers

1


Friend, if the id field is autoincrement then you do not need to declare them in your Sert.
For example, imagine you have a fruit table:

CREATE TABLE frutas(
   id SERIAL PRIMARY KEY,
   nome VARCHAR NOT NULL
);

And the Insert would look like this:

INSERT INTO frutas(nome) VALUES('laranja');

Note that I did not inform the id field on Insert.
In your case it would have to be so:

INSERT INTO cliente_sub_grupo (id_cliente,subgrupo,tipo_atendimento,ind_valor_titular_igual_contrato,ind_valor_dependente_igual_contrato,valor_titular,valor_dependente)  VALUES (:id_cliente,:subgrupo,:tipo_atendimento,:ind_valor_titular_igual_contrato,:ind_valor_dependente_igual_contrato,:valor_titular,:valor_dependente)
  • The error was not in the INSERT. This Insert I posted was from the console of a gambiara of another company that passed this job from a library called Trust. Running the test directly in the database (INSERT) even leaving the id field blank, gave the same error. It was an error in the table, which was corrected as I mentioned above.

0

@Murilo Portugal

I’ve solved, I’ve done all of the things you suggested and other tests here, but the mistake is that in the field id in the column Default, as in the following image, the:

nextval('pessoa_id_pessoa_seq'::regclass)

inserir a descrição da imagem aqui

And it worked.

  • If you declare the field as SERIAL then this is implied. See the explanation of the type of SERIAL data at: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL

  • 1

    @Ramos, exactly as the friend above said, to use autoincrement use the SERIAL in postgre, so it is implicit, in my reply I considered that you were already using it, which is the most common. But anyway I’m glad it worked out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.