Skip to content

Conversation

ting-lan-wang
Copy link
Contributor

Description

Fixes TestForeignKeyConstraints and TestForeignKeyConstraintsBelongsTo.

  1. Change the tag from uniqueIndex to unique

Oracle does not treat a unique index the same as a unique constraint.
When a column is tagged with uniqueIndex, GORM generates a unique index, but foreign key constraints in Oracle require the referenced column to be backed by a unique constraint.

For example, given the following structs:

	type Profile struct {
		ID       uint
		Name     string
		MemberID uint
	}

	type Member struct {
		ID      uint
		Refer   uint `gorm:"uniqueIndex"`
		Name    string
		Profile Profile `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;FOREIGNKEY:MemberID;References:Refer"`
	}

the generated SQL is:

CREATE TABLE "members" ("id" NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,"refer" NUMBER(20),"name" VARCHAR2(4000),PRIMARY KEY ("id"))

CREATE TABLE "profiles" ("id" NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,"name" VARCHAR2(4000),"member_id" NUMBER(20),PRIMARY KEY ("id"),CONSTRAINT "fk_members_profile" FOREIGN KEY ("member_id") REFERENCES "members"("refer") ON DELETE CASCADE)

Which fails with:
ORA-02270: no matching unique or primary key for this column-list

Changing the tag to unique generates the correct SQL:

CREATE TABLE "members" ("id" NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,"refer" NUMBER(20),"name" VARCHAR2(4000),PRIMARY KEY ("id"),CONSTRAINT "uni_members_refer" UNIQUE ("refer"));

CREATE TABLE "profiles" ("id" NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,"name" VARCHAR2(4000),"member_id" NUMBER(20),PRIMARY KEY ("id"),CONSTRAINT "fk_members_profile" FOREIGN KEY ("member_id") REFERENCES "members"("refer") ON DELETE CASCADE)
  1. Support OnUpdate

Unlike other databases, Oracle does not directly support the ON UPDATE foreign key constraint. To work around this, the driver generates a trigger that simulates the ON UPDATE behavior. When a constraint is tagged with OnUpdate, the driver skips generating the unsupported SQL and instead creates a trigger that cascades updates whenever the referenced column in the parent table changes.

The trigger is automatically dropped when the table is dropped or when DropConstraint() is called.

Added a test TestMigrateOnUpdateConstraint to verify this feature.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Sep 10, 2025
@ting-lan-wang ting-lan-wang marked this pull request as draft September 11, 2025 13:56
@ting-lan-wang ting-lan-wang marked this pull request as ready for review September 11, 2025 18:13
@ting-lan-wang ting-lan-wang merged commit e72806f into main Sep 12, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants