Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

ON DELETE / ON UPDATE of inline foreign keys lost with file input (-i)

Open Beginner friendly
#1,981 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
mysql, perl, postgresql, sql
Domain
databases, tooling

Research direction

Start in Ora2Pg.pm around _parse_constraint() at line 3060, then follow how _create_foreign_keys() uses DELETE_RULE and UPDATE_RULE. Reproduce with the t.sql example and the ora2pg file-input command; done means inline CASCADE, SET NULL, and MySQL ON UPDATE CASCADE are preserved while unchanged constraints retain their existing output.

Written by the indexing model from the issue text.

Description

While testing the IDENTITY fix I noticed another one with file input: a foreign key declared inside CREATE TABLE always comes out as ON DELETE NO ACTION, whatever the source says. Oracle, MySQL and MSSQL input all do it, current master (d53a3df):

$ cat t.sql
CREATE TABLE po (id NUMBER NOT NULL, CONSTRAINT po_pk PRIMARY KEY (id));
CREATE TABLE co1 (id NUMBER NOT NULL, pid NUMBER, CONSTRAINT co1_fk FOREIGN KEY (pid) REFERENCES po (id) ON DELETE CASCADE);
CREATE TABLE co2 (id NUMBER NOT NULL, pid NUMBER, CONSTRAINT co2_fk FOREIGN KEY (pid) REFERENCES po (id) ON DELETE SET NULL);
$ ora2pg -c /dev/null -i t.sql -t TABLE -o out.sql
$ grep 'FOREIGN KEY' out.sql
ALTER TABLE co1 ADD CONSTRAINT co1_fk FOREIGN KEY (pid) REFERENCES po(id) ON DELETE NO ACTION INITIALLY IMMEDIATE;
ALTER TABLE co2 ADD CONSTRAINT co2_fk FOREIGN KEY (pid) REFERENCES po(id) ON DELETE NO ACTION INITIALLY IMMEDIATE;

The schema loads fine, so nothing warns you, but deleting a parent now fails instead of cascading:

postgres=# INSERT INTO po VALUES (1);
postgres=# INSERT INTO co1 VALUES (10, 1);
postgres=# DELETE FROM po WHERE id = 1;
ERROR:  update or delete on table "po" violates foreign key constraint "co1_fk" on table "co1"
DETAIL:  Key (id)=(1) is still referenced from table "co1".

The ALTER TABLE ... ADD CONSTRAINT ... ON DELETE CASCADE form is fine, only the inline one is affected: _parse_constraint() pushes empty strings for DELETE_RULE and UPDATE_RULE (Ora2Pg.pm:3060), so _create_foreign_keys() falls back to NO ACTION.

This fixes it here:

 		$novalidate = 'NOT VALIDATED' if ($c =~ /NOVALIDATE/);
+		my $delete_rule = '';
+		$delete_rule = uc($1) if ($c =~ /\bON\s+DELETE\s+(CASCADE|SET\s+NULL|SET\s+DEFAULT|RESTRICT|NO\s+ACTION)\b/is);
+		$delete_rule =~ s/\s+/ /g;
+		my $update_rule = '';
+		$update_rule = uc($1) if ($c =~ /\bON\s+UPDATE\s+(CASCADE|SET\s+NULL|SET\s+DEFAULT|RESTRICT|NO\s+ACTION)\b/is);
+		$update_rule =~ s/\s+/ /g;
 		# CONSTRAINT_NAME,R_CONSTRAINT_NAME,SEARCH_CONDITION,DELETE_RULE,$deferrable,DEFERRED,R_OWNER,TABLE_NAME,OWNER,UPDATE_RULE,VALIDATED
-		push(@{$self->{tables}{$tb_name}{foreign_key}}, [ ($c_name,'','','',$deferrable,$deferred,'',$tb_name,'','',$novalidate) ]);
+		push(@{$self->{tables}{$tb_name}{foreign_key}}, [ ($c_name,'','',$delete_rule,$deferrable,$deferred,'',$tb_name,'',$update_rule,$novalidate) ]);

Tested on PG 16 with Oracle, MySQL (-m) and MSSQL (-M) input: CASCADE and SET NULL are kept and behave as in the source, ON UPDATE CASCADE from MySQL too. FKs without a rule still get NO ACTION, DEFERRABLE INITIALLY DEFERRED is unchanged, and the ALTER TABLE form gives the same output as before.

Dominant language
Perl
Stars
1.2k
Forks
380
Avg merge
6h 55m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from darold/ora2pg

All issues in darold/ora2pg

Similar issues

More Perl issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.