2

Googling around told me to try this:

insert into Plan default values;

but I got:

null value in column "id" violates not-null constraint

or this:

insert into Plan (id) values (null);

but I got the same thing.

The examples were for SQL: is there something different for PostgreSQL?

Table structure:

  create table Plan (
    id int4 not null,
    primary key (id)
); 
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Cartesian Theater
  • 1,920
  • 2
  • 29
  • 49

2 Answers2

3

Your column does not accept NULL values (defined not null), and you did not define a default, either. So you must provide an integer value:

insert into plan (id) values (1235); -- any integer value

Most probably you really want a serial column, which draws a default value from a sequence:

Your table could look like this:

CREATE TABLE plan (plan_id serial PRIMARY KEY); 

An the INSERT could be:

INSERT INTO plan DEFAULT VALUES;
Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
1

You should create a sequence and set the default as nextval like so:

create sequence plan_sequence
start 100
increment 1
;

  create table plan (
    id int4 not null default nextval('plan_sequence'),
    primary key (id)
); 

A sequence provides an auto-incrementing value. It is used for primary keys, etc.

Walker Farrow
  • 3,579
  • 7
  • 29
  • 51