go-gorm / go-gorm/postgres

HasConstraint ignores UniqueName from custom namer

Open
#316 0 comments 0 reactions 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
297
Forks
155
Avg merge
27m
Merged PRs (30d)
2

Description

## 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.

```go
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.