W
W
WhatIsHTML2018-09-01 15:36:23
PostgreSQL
WhatIsHTML, 2018-09-01 15:36:23

PostgreSQL not working id autoincrement?

Updated the database and now an error occurs when trying to insert a new record into the `user_artist` table.

create table user_artist
(
  "id" serial primary key,
  "userId" serial REFERENCES user (id) on delete cascade,
  "artistId" serial REFERENCES artist (id) on delete cascade,
  "added" timestamp,
  unique ("userId", "artistId")
);

Error text
{ error: нулевое значение в столбце "id" нарушает ограничение NOT NULL
0|index    |     at Connection.parseE (/home/www.example.com/node_modules/pg/lib/connection.js:553:11)
0|index    |     at Connection.parseMessage (/home/www.example.com/node_modules/pg/lib/connection.js:378:19)
0|index    |     at Socket.<anonymous> (/home/www.example.com/node_modules/pg/lib/connection.js:119:22)
0|index    |     at Socket.emit (events.js:182:13)
0|index    |     at Socket.EventEmitter.emit (domain.js:442:20)
0|index    |     at addChunk (_stream_readable.js:280:12)
0|index    |     at readableAddChunk (_stream_readable.js:265:11)
0|index    |     at Socket.Readable.push (_stream_readable.js:220:10)
0|index    |     at TCP.onread (net.js:638:20)
0|index    |   name: 'error',
0|index    |   length: 293,
0|index    |   severity: 'ОШИБКА',
0|index    |   code: '23502',
0|index    |   detail: 'Ошибочная строка содержит (null, 1, 725142, 2018-09-01 12:22:55).',
0|index    |   hint: undefined,
0|index    |   position: undefined,
0|index    |   internalPosition: undefined,
0|index    |   internalQuery: undefined,
0|index    |   where: undefined,
0|index    |   schema: 'public',
0|index    |   table: 'user_artist',
0|index    |   column: 'id',
0|index    |   dataType: undefined,
0|index    |   constraint: undefined,
0|index    |   file: 'execMain.c',
0|index    |   line: '2008',
0|index    |   routine: 'ExecConstraints' }

Here is the table query
INSERT INTO user_artist ("userId", "artistId", added) VALUES ($(userId), $(artistId), $(added)) 
ON CONFLICT DO NOTHING;

The input data, as you can see from the error, looks correct except for the first null value `(null, 1, 725142, 2018-09-01 12:22:55)`. The `user_artist` table, like `user`, was taken from another database, data integrity seems to be in order, but as far as I understand, the problem is in user_artist.id, which should be calculated automatically, but apparently ceased due to the transfer of the table. How to fix a jamb?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zanak, 2018-09-01
@Zanak

The request sends 3 values, and the message has 4 elements, perhaps the ORM intervenes?
Try explicitly setting the id so that the request looks like this:

INSERT INTO user_artist (id, "userId", "artistId", added) 
VALUES (default, $(userId), $(artistId), $(added)) 
ON CONFLICT DO NOTHING;

well, or dig into the documentation about default values, if we are still talking about ORM.

K
krypt3r, 2018-09-03
@krypt3r

Look for why null is inserted into id, and not DEFAULT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question