diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql index 85ae350f4594..35d8096b440d 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql @@ -13,5 +13,12 @@ -- Drop the NOT NULL constraint on user_id column ALTER TABLE process ALTER COLUMN user_id DROP NOT NULL; +-- Sets null the invalid user_id inside the process table +UPDATE process +set user_id = null +where not exists( + select 1 from eperson where eperson.uuid = user_id +); + -- Add the foreign key constraint with ON DELETE SET NULL ALTER TABLE process ADD CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES eperson (uuid) ON DELETE SET NULL; \ No newline at end of file diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql index 85ae350f4594..35d8096b440d 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.04.23__alter_table_process_add_foreign_key_on_user_id.sql @@ -13,5 +13,12 @@ -- Drop the NOT NULL constraint on user_id column ALTER TABLE process ALTER COLUMN user_id DROP NOT NULL; +-- Sets null the invalid user_id inside the process table +UPDATE process +set user_id = null +where not exists( + select 1 from eperson where eperson.uuid = user_id +); + -- Add the foreign key constraint with ON DELETE SET NULL ALTER TABLE process ADD CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES eperson (uuid) ON DELETE SET NULL; \ No newline at end of file