0

For the last couple of days I have migrated from MySQL to Postgres. But without any success. I have now ported my MySQL database to Postgres but I was using UUID's in MySQL in a VARCHAR. Now I want to go to postgres but it seems impossible for me to transfer the varchar in postgres to UUID. Because the database is not empty there are foreign key constraints. I thought I could turn them all off with:

set session_replication_role to replica;

I also tried to turn off all triggers for the tables. For example, this is the query I try to run:

ALTER TABLE building_block_buildingblock DISABLE TRIGGER ALL;
ALTER TABLE building_block_buildingblockhistory DISABLE TRIGGER ALL;
ALTER TABLE building_block_buildingblockhistory ALTER COLUMN id SET DATA TYPE UUID USING (uuid_generate_v4());
ALTER TABLE building_block_buildingblock ALTER COLUMN id SET DATA TYPE UUID USING (uuid_generate_v4());
ALTER TABLE building_block_buildingblockhistory ALTER column building_block_id SET DATA TYPE UUID USING (uuid_generate_v4());

But I get the error:

SQL Error [42804]: ERROR: foreign key constraint "building_block_buildingbl_building_block_id_e08cbc73_fk" cannot be implemented
  Detail: Key columns "building_block_id" and "id" are of incompatible types: character varying and uuid.

I also tried wrapping it in a function as shown here: PostgreSQL - disabling constraints

This is the function I tried:

CREATE OR REPLACE FUNCTION f() RETURNS void AS
$BODY$
begin
set session_replication_role to replica;
SET CONSTRAINTS ALL deferred;
ALTER TABLE building_block_buildingblock DISABLE TRIGGER ALL;
ALTER TABLE building_block_buildingblockhistory DISABLE TRIGGER ALL;
ALTER TABLE building_block_buildingblockhistory ALTER COLUMN id SET DATA TYPE UUID USING (uuid_generate_v4());
ALTER TABLE building_block_buildingblock ALTER COLUMN id SET DATA TYPE UUID USING (uuid_generate_v4());
ALTER TABLE building_block_buildingblockhistory ALTER column building_block_id SET DATA TYPE UUID USING (uuid_generate_v4());
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  
select f();

Is there a way to do this migration I want to do? The only thing I can think of now is manually adjusting my SQL dump file. But that does not only sound like a lot of work but also error prone.

3
  • The statements you posted change the column types, they don't disable any constraints Commented Sep 29, 2020 at 7:48
  • Does this answer your question? PostgreSQL - disabling constraints Commented Sep 29, 2020 at 7:49
  • I updated my answer. It does not seem to help Commented Sep 29, 2020 at 13:06

1 Answer 1

0

The following will certainly disable all foreign keys:

SET session_replication_role TO replica;

It can only be run as a superuser.

There is one exception: a trigger on which you used

ALTER TABLE ... ENABLE REPLICA|ALWAYS TRIGGER ...

will fire even if session_replication_role is set to replica.

Sign up to request clarification or add additional context in comments.

11 Comments

See question. I already tried that. It didn't work. I do have superuser access.
Then you didn't try it right. Using SET changes the setting only in the current database session.
I updated my question with the function I tried. I know it is only for the current database session. That's what I found weird.
That will work fine as long as you call the function in the same database session as the operations that violate the foreign keys. There is no need to explicitly disable triggers on certain tables. Actually, there is a caveat - see my updated answer.
I can send you my database if you want to? I will remove all the sensitive data. I understand if you do not have the time
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.