Skip to content
New issue

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

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

Already on GitHub? # to your account

Automigrate error caused by indexes while using dynamic table name #4752

Closed
neeroz opened this issue Oct 7, 2021 · 5 comments · Fixed by #4773
Closed

Automigrate error caused by indexes while using dynamic table name #4752

neeroz opened this issue Oct 7, 2021 · 5 comments · Fixed by #4773
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@neeroz
Copy link

neeroz commented Oct 7, 2021

GORM Playground Link

go-gorm/playground#386

Description

I am trying to migrate tables using dynamic table name. All these tables have different name but the same schema. I am using scopes to generate the dynamic table name.

The tables are created with the dynamic table name but the indexes are not created as it should.

Struct
type NewUser struct {
gorm.Model
Name string gorm:"index"
City string gorm:"index"
Table string gorm:"-"
}

tableNameFormat newuser_%x

tableName := []string{"a","b","c"}

table created successfully newuser_a, newuser_b,newuser_c

When using scopes to generate the dynamic table name, the indexes are formatted as idx_new_users_name for all the tables and thus error -> Failed, got error: index idx_new_users_name already exists

To Reproduce Error: ./test.sh

Expected Output:

While doing migration, the tables with dynamic names should be created along with its indexes.

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Oct 7, 2021
@neeroz neeroz changed the title Automigrate error caused by indexes while generating dynamic table name Automigrate error caused by indexes while using dynamic table name Oct 7, 2021
@ghost
Copy link

ghost commented Oct 8, 2021

@neeroz hello,this may be a problem with sqlite,It does not support indexes with the same name.

@neeroz
Copy link
Author

neeroz commented Oct 8, 2021

@longlihale This problem persists with postgresql as well. The index format idx_<table-name>_<column-name> is not being generated dynamically (table-name) which causes error. I can confirm this error on sqlite and postgresql
Screen Shot 2021-10-08 at 11 44 59 AM
.

@ghost
Copy link

ghost commented Oct 9, 2021

@neeroz thanks,this is bug and will be fixed later.

@xwjdsh
Copy link
Contributor

xwjdsh commented Oct 12, 2021

Sorry, I tried to solve this problem, but it led to more mistakes, at last, I have no way... (I should make a pull request after full testing)

@ghost
Copy link

ghost commented Oct 12, 2021

Sorry, I tried to solve this problem, but it led to more mistakes, at last, I have no way... (I should make a pull request after full testing)

because the problem happen in Schema result in it have some tough....

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
3 participants