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

Refactor: Distinguish between unique and UniqueIndex #6381

Closed
5 tasks done
a631807682 opened this issue Jun 6, 2023 · 10 comments
Closed
5 tasks done

Refactor: Distinguish between unique and UniqueIndex #6381

a631807682 opened this issue Jun 6, 2023 · 10 comments

Comments

@a631807682
Copy link
Member

a631807682 commented Jun 6, 2023

Describe the feature

Distinguish between unique index and index to better support them.

Motivation

Because we can't distinguish them at present, it causes such as #6224 (comment), and column will not be able to set unique index and index at the same time

The following tasks are open

  • Distinguish between unique index and index in gorm @black-06
  • Distinguish between unique index and index in postgres @black-06
  • Distinguish between unique index and index in sqlserver @black-06
  • Distinguish between unique index and index in sqlite @black-06
  • Distinguish between unique index and index in mysql @black-06

If anyone is interested in the above tasks, please leave a message in the comments.

Related Issues

related issues:
#6224
#5401
#5715
#5681
#6378
#6407
related pull request:
#5341

@black-06
Copy link
Contributor

black-06 commented Jun 6, 2023

Let me do the distinguish in gorm (I touched the checkbox by mistake

@black-06
Copy link
Contributor

black-06 commented Jun 6, 2023

Is the index created repeatedly because of this? (#5715 & #6224)
Or is there any fix in Dialector ?

Here's a playground I created: go-gorm/playground#609
It says no duplicate indexes are created.

@a631807682
Copy link
Member Author

Is the index created repeatedly because of this? (#5715 & #6224) Or is there any fix in Dialector ?

Here's a playground I created: go-gorm/playground#609 It says no duplicate indexes are created.

Here is a mysql example

type Test struct {
	ID   uint   `gorm:"primaryKey"`
	Name string `gorm:"type:varchar(10);uniqueIndex"`
}

type Test1 struct {
	ID   uint   `gorm:"primaryKey"`
	Name string `gorm:"type:varchar(20);uniqueIndex"`
}

func TestGORM(t *testing.T) {
	DB.Migrator().DropTable(&Test{})
	DB.AutoMigrate(&Test{})
	DB.Table("tests").AutoMigrate(&Test1{})

	indexs, err := DB.Migrator().GetIndexes(&Test{})
	if err != nil {
		t.Fatal(err)
	}

	if len(indexs) != 2 { // pk and unique index
		t.Fatalf("should have one index, but got %v", len(indexs))
	}
}
[rows:0] ALTER TABLE `tests` MODIFY COLUMN `name` varchar(20) UNIQUE
[rows:0] ALTER TABLE `tests` MODIFY COLUMN `name` varchar(10) UNIQUE

We also need to pay attention to such scenarios

type Test2 struct {
	ID   uint   `gorm:"primaryKey"`
	Name string `gorm:"type:varchar(20);uniqueIndex:uniq_name_code;index"` // index and uniqueIndex
	Code string `gorm:"type:varchar(20);uniqueIndex:uniq_name_code"`
}

@a631807682
Copy link
Member Author

There is another situation here. Since unique and unique index cannot be distinguished, Description will be specified as unique when table create (will create description index), which leads to a conflict with partial Indexes user_description_exclude_deleted.

#6378

@black-06
Copy link
Contributor

black-06 commented Jun 7, 2023

I will continue to work on mysql and postgres

@black-06
Copy link
Contributor

When all the drivers are completed and tested,
I can confirm that the changes in gorm are not breaking.

So continue to do sqlite and sqlserver...

@a631807682
Copy link
Member Author

When all the drivers are completed and tested, I can confirm that the changes in gorm are not breaking.

So continue to do sqlite and sqlserver...

Thank you for your contribution

@vijaykramesh
Copy link

any update on this? I can confirm from black-06's branch and the postgres driver it works for me, in the meantime we can't upgrade gorm beyond 1.24.2 due to this issue (but would like to do so in order to get some nested join fixes)

@a631807682
Copy link
Member Author

@jinzhu @saeidee cc

@black-06
Copy link
Contributor

black-06 commented Feb 6, 2024

I am pleased to announce that we have completed the work.

But note that gorm v1.25.7 should work with (or higher version) sqlite v1.5.5, mysql v1.5.4, postgres v1.5.6, sqlserver v1.5.3

otherwise there may be some unexpected problems.

Similarly, these (or higher) versions of drivers require gorm v1.25.7 (or higher)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants