Postgres Foreign Data Wrapper: insert to mysql table primary key increment issue

Question:

I am using PostgreSQL foreign data wrapper Multicorn (which uses SQLAlchemy) to map tables from external MySQL database. Everything seems to be working fine so far, except one issue: if I run an insert query on my foreign mysql table without specifying primary ID, because it’s set to auto increment, foreign data wrapper tries to append and starts the increment from 1 even though such a key already exists in the remote MySQL table. This is better explained by example:

MySQL table:

CREATE TABLE `test` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `value` VARCHAR(100) NULL DEFAULT NULL,
  PRIMARY KEY (`id`));

MySQL table data:

select * from test;
+----+-------+
| id | value |
+----+-------+
|  1 | val1  |
|  2 | val2  |
|  3 | val3  |
+----+-------+

PostgreSQL FDW server:

CREATE SERVER test foreign data wrapper multicorn options (wrapper 'multicorn.sqlalchemyfdw.SqlAlchemyFdw');

PostgreSQL FDW table:

create foreign table test1 (
    id serial,
    value character varying
) server test options (
    tablename 'test',
    db_url 'mysql://root:[email protected]/test',
    primary_key 'id'
);

This is the insert statement that I’ve tried:

insert into test1 (value) values ('val4');

It throws this error:

ERROR:  Error in python: IntegrityError
DETAIL:  (IntegrityError) (1062, "Duplicate entry '1' for key 'PRIMARY'") 'INSERT INTO test (id, value) VALUES (%s, %s)' (1L, u'val4')

For some reason there is an attempt to manually insert a primary ID which I’ve specified as auto increment on both databases/tables. Why is this happening and how can I fix it? What is causing this, is it Postgres, FDW plugin Multicorn or SQLAlchemy?

Asked By: Caballero

||

Answers:

What if you use int instead of serial on the PostgreSQL side?

create foreign table test1 (
    id int,
    value character varying
) server test options (
    tablename 'test',
    db_url 'mysql://root:[email protected]/test',
    primary_key 'id'
);
Answered By: Kouber Saparev

can you help me, please? I have a related issue.
First of all, I have a question. There is a later version where we are creating a foreign table in the option field we right "table" instead of "tablename"?
In my work, we have an old oracle database with all alphanumeric information. To join alphanumeric information with geometric information the company creates back then foreign tables in PostgresSQl of some Oracle tables it works fine.
Now we are trying to create a plugin for qgis that will do some geometric analysis and insert alphanumeric data in the Oracle database using one foreign table of PostgresSQl. And there comes the problem. We cannot deal with the sequence id in the remote(Oracle) table.
In my remote machine, I solve the problem by creating the foreign table with all the columns except the id! So when I do the insertion omit the id field and the sequence in the remote column works properly.
However in PostgreSQL in work, the foreign table has strange behavior, if I omit the id all columns sift left :ยด(!. I really think is because of the version of pwd extension or something.
There is an example of how the table is created:
Work foreign table:

CREATE FOREIGN TABLE si.tbl_accaofiscal(
idaccaofiscal numeric OPTIONS (key ‘true’) NOT NULL, (this is the problem field)
identidadepredial numeric NOT NULL,
idreparticaofinancas numeric NOT NULL,
idtpaccaofiscal numeric NOT NULL,
descricao character varying(255) NULL COLLATE pg_catalog."default",
)
SERVER oracle_si
OPTIONS (schema ‘SI_As’, table ‘TBL_ACCAOFISCAL’);

Remote foreing table:

CREATE FOREIGN TABLE si.tbl_accaofiscal(
idaccaofiscal numeric OPTIONS (key ‘true’) NOT NULL, (i omit this value and everything works properly)
identidadepredial numeric NOT NULL,
idreparticaofinancas numeric NOT NULL,
idtpaccaofiscal numeric NOT NULL,
descricao character varying(255) NULL COLLATE pg_catalog."default",
)
SERVER oracle_si
OPTIONS (schema_name ‘SI_As’, table_name ‘TBL_ACCAOFISCAL’);

As you see the difference is in the Options field, in my localhost this field does not accept either "scheme" or "table".

Answered By: Joao Folgado