go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly

Home Page:https://gorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoMigrate method creates incorrect index name

y-okuno opened this issue · comments

Description:

I encountered an issue with GORM's AutoMigrate function when upgrading to the latest versions.

Successful migration with:

  • gorm.io/driver/postgres v1.4.8
  • gorm.io/gorm v1.25.5

Failed migration with:

  • gorm.io/driver/postgres v1.5.9
  • gorm.io/gorm v1.25.10

In the latest version, it seems that the index name specified in the model is not being respected. The AutoMigrate function creates an incorrect index name which leads to a migration failure.

Steps to Reproduce

  1. Use the following versions:

    • gorm.io/driver/postgres v1.5.9
    • gorm.io/gorm v1.25.10
  2. Define the model as follows:

    type Master struct {
        ID         uint32 `gorm:"primaryKey"`
        IPv4Prefix string `gorm:"uniqueIndex"`
    }
  3. Create the table in PostgreSQL with the following SQL:

CREATE TABLE public.masters (
    id bigserial NOT NULL,
    ipv4_prefix text NULL,
    CONSTRAINT idx_masters_ipv4_prefix UNIQUE (ipv4_prefix)
);
  1. Execute AutoMigrate method with the above model.

  2. Also tried with the following model definition, but the result was the same:

type Master struct {
    ID         uint32 `gorm:"primaryKey"`
    IPv4Prefix string `gorm:"uniqueIndex:idx_masters_ipv4_prefix"`
}

Expected Behavior

The migration should succeed without errors.

Actual Behavior

The migration fails with the following error:

ERROR: constraint "uni_masters_ipv4_prefix" of relation "masters" does not exist (SQLSTATE 42704)

It appears that AutoMigrate is attempting to create a constraint named uni_masters_ipv4_prefix instead of respecting the specified idx_masters_ipv4_prefix .

Environment

  • GORM version: v1.25.10
  • GORM Postgres driver version: v1.5.9
  • PostgreSQL version: 13.4
  • Go version: 1.21.1

Additional Context

This issue does not occur with the following versions:

  • gorm.io/driver/postgres v1.4.8
  • gorm.io/gorm v1.25.5

It seems that the index name specified in the struct tag is not being applied correctly in the latest version.

Thank you for your attention to this issue. Please let me know if you need any further information or assistance in reproducing the issue.

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking