Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoMigrate with Many2Many fails when using UUID for ID #5320

Closed
shi-rudo opened this issue May 5, 2022 · 9 comments · Fixed by #5322
Closed

AutoMigrate with Many2Many fails when using UUID for ID #5320

shi-rudo opened this issue May 5, 2022 · 9 comments · Fixed by #5322
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@shi-rudo
Copy link

shi-rudo commented May 5, 2022

GORM Playground Link

https://github.com/shi-rudo/playground-1
go-gorm/playground#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
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label May 5, 2022
@github-actions
Copy link

github-actions bot commented May 5, 2022

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

@shi-rudo
Copy link
Author

shi-rudo commented May 5, 2022

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

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

@github-actions
Copy link

github-actions bot commented May 5, 2022

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

@shi-rudo
Copy link
Author

shi-rudo commented May 5, 2022

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

go-gorm/playground#474

@github-actions
Copy link

github-actions bot commented May 5, 2022

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

@github-actions github-actions bot added status:stale type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps status:stale labels May 5, 2022
@shi-rudo
Copy link
Author

shi-rudo commented May 6, 2022

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

@a631807682
Copy link
Member

Add primarykey tag can solve it

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

@onurhuseyincantay
Copy link

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

@a631807682
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants