Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBeaver does not keep primary keys on import/export

Tags:

dbeaver

I'm using DBeaver to migrate data from Postgres to Derby. When I use the wizard in DBeaver to go directly from one table to another, the primary key in Derby is being generated instead of inserted. This causes issues on foreign keys for subsequent tables.

If I generate the SQL, the primary key is part of the SQL statement and is properly inserted. However there are too many rows to handle in this way.

Is there a way to have DBeaver insert the primary key instead of letting it be generated when importing / exporting directly to database tables?

Schema of target table

CREATE TABLE APP.THREE_PHASE_MOTOR (
    ID BIGINT NOT NULL DEFAULT GENERATED_BY_DEFAULT,
    VERSION INTEGER NOT NULL,
    CONSTRAINT SQL130812103636700 PRIMARY KEY (ID)
);

CREATE INDEX SQL160416184259290 ON APP.THREE_PHASE_MOTOR (ID);

Schema of source table

CREATE TABLE public.three_phase_motor (
    id int8 NOT NULL DEFAULT nextval('three_phase_motor_id_seq'::regclass),
    "version" int4 NOT NULL,
    CONSTRAINT three_phase_motor_pkey PRIMARY KEY (id)
)
WITH (
    OIDS=FALSE
);
like image 342
Timothy Vogel Avatar asked Apr 16 '16 23:04

Timothy Vogel


People also ask

How do you find the primary key of a table in DBeaver?

To have a look at the primary keys of all the tables, in the Database Navigator, go to your database and then right click on public -> select View diagram. This will open the ER diagram for the whole database and the connections between the tables. Here, under each table name, the bold columns are the primary keys.


2 Answers

When you go to export, check the Include generated column option, and the primary key (auto-incremented) will be included in the export.

See this for more details: https://github.com/dbeaver/dbeaver/commit/d1f74ec88183d78c7c6620690ced217a52555262

Personally I think this needs to be more clear, and why they excluded it in the first place was not good data integrity.

Include generated column

like image 93
Sarke Avatar answered Sep 20 '22 21:09

Sarke


I found a trick working with version 6.0.5; do these steps:

  • double click a table name
  • then select Data tab
  • then click the gray table corner (the one on top of row order numbers) in order to select all rows
  • then right click the same gray table corner
  • then select Generate SQL -> INSERT menu

a window with the INSERT instructions including id (primary key) will popup.

PS: when selecting a subset of rows the same menu would work for only those too

enter image description here

like image 44
adrhc Avatar answered Sep 20 '22 21:09

adrhc