go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.2k stars 3.88k forks source link

AutoMigrate method creates incorrect index name #7100

Open y-okuno opened 2 weeks ago

y-okuno commented 2 weeks ago

Description:

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

Successful migration with:

Failed migration with:

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)
    );
  4. Execute AutoMigrate method with the above model.

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

Additional Context

This issue does not occur with the following versions:

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.

github-actions[bot] commented 2 weeks ago

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

dashrews78 commented 2 days ago

My team just experienced the same problem.

The constraint is not present because GORM is calculating the name wrong. I'm also wondering if the dropping of the constraint shouldn't also have IF EXISTS as that would have stopped the migration from failing.

github-actions[bot] commented 2 days ago

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