GORM Playground Link

https://github.com/shi-rudo/playground-1 https://github.com/go-gorm/playground/pull/474

for the sake of simplicity I have created a reproduction sample https://github.com/shi-rudo/gorm-automigrate-many2many-uuid-fail

Description

When I use UUID as type for the gorm ID the automigrator failed at every app start except the first one. With Integer, for example, the restart is without errors.

WITH POSTGRES:

type User struct {
    gorm.Model
    ID        uuid.UUID  `gorm:"type:uuid;default:uuid_generate_v4()"`
    Languages []Language `gorm:"many2many:user_languages;"`
}

type Language struct {
    gorm.Model
    ID   uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
    Name string
}

func main() {
    dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", "localhost", "gorm", "gorm", "gorm", "9920", "disable", "Etc/UTC")
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }

    db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")

    // Migrate the schema
    err = db.AutoMigrate(
        &User{},
        &Language{},
    )
    if err != nil {
        panic("failed to migrate")
    }
}

console from reproduction sample:

2022/05/05 12:48:08 /Users/xxx/go/pkg/mod/gorm.io/driver/postgres@v1.3.5/migrator.go:282 ERROR: column "user_id" is in a primary key (SQLSTATE 42P16)
[0.937ms] [rows:0] ALTER TABLE "user_languages" ALTER COLUMN "user_id" DROP NOT NULL
panic: failed to migrate

console from playground:

2022/05/05 13:06:11 /Users/xxx/Devlopment/projects/archived/learning/gorm/playground/db.go:97 near "(": syntax error
[0.021ms] [rows:0] CREATE TABLE `users` (`id` uuid DEFAULT uuid_generate_v4(),`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text,`age` integer,`birthday` datetime,`company_id` integer,`manager_id` uuid,`active` numeric,PRIMARY KEY (`id`),CONSTRAINT `fk_users_company` FOREIGN KEY (`company_id`) REFERENCES `companies`(`id`),CONSTRAINT `fk_users_team` FOREIGN KEY (`manager_id`) REFERENCES `users`(`id`))
2022/05/05 13:06:11 Failed to auto migrate, but got error near "(": syntax error

Comment From: github-actions[bot]

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.io ✨ Search Before Asking

Comment From: shi-rudo

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.io ✨ Search Before Asking

https://github.com/shi-rudo/playground-1

Comment From: github-actions[bot]

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.io ✨ Search Before Asking

Comment From: shi-rudo

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.io ✨ Search Before Asking

https://github.com/go-gorm/playground/pull/474

Comment From: github-actions[bot]

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.io ✨ Search Before Asking

Comment From: shi-rudo

I was able to narrow down the problem a bit, UUID can be used if you exclude the tag default:uuid_generate_v4() (uuid-ossp is activated on Postgres).

Comment From: a631807682

Add primarykey tag can solve it

ID        uuid.UUID  `gorm:"type:uuid;default:uuid_generate_v4();primarykey"`

Comment From: onurhuseyincantay

@a631807682 @jinzhu I investigated the PR #5322 and tried to do the exact same thing on my side I still get an error like below:

column "language_id" of relation "user_languages" does not exist (SQLSTATE 42703)

are we sure the issue has been fixed from what I understood primaryKey should be enough to overcome this issue.

Comment From: a631807682

@a631807682 @jinzhu I investigated the PR #5322 and tried to do the exact same thing on my side I still get an error like below:

column "language_id" of relation "user_languages" does not exist (SQLSTATE 42703)

are we sure the issue has been fixed from what I understood primaryKey should be enough to overcome this issue.

There have been many changes in the latest version, but the tests for this PR did not fail, and the problems you encountered are not necessarily due to it. We recommend that you use the latest version for testing. If you still have problems, please provide a reproduction link or a more detailed description.