Skip to content

HasConstraint ignores UniqueName from custom namer #316

@hypherionmc

Description

@hypherionmc

GORM Playground Link

Not really sure how I would submit a playground link for this, since it's a very weird case, and for the testing to work, it needs to be done on an existing database from another ORM like mikro-orm

Description

I am rewriting the backend to one of my apps in GO from Typescript. While testing, I noticed that gorm was creating duplicate UNIQUE indexes as well as duplicate foreign keys.

I figured out this was because of the fact that I am using a database previously generated by Mikro-ORM, and the naming differs.

I ended up creating a custom Namer to fix most of it, but one specific one fails.

func (ns CustomMigrator) UniqueName(table, column string) string {
	value := ns.NamingStrategy.UniqueName(table, column)
	value = strings.Replace(value, "uni_", "", 1) + "_unique"
	println(value)
	return value
}

ignore the messy code, this is just for testing

When running my application, I am greeted with this error:

[0.277ms] [rows:0] ALTER TABLE "dependency_type" ADD CONSTRAINT "dependency_type_key_unique" UNIQUE ("key")
panic: ERROR: relation "dependency_type_key_unique" already exists (SQLSTATE 42P07)

Which means that it is using my name correctly, but somehow isn't seeing that the constraint already exists on the database.

So far, this is the only part coupled with namer that I could see that doesn't work

EDIT: It appears that when GORM completely creates the tables from scratch, it works correctly. The issue only appears on existing databases

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions