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

Composite Primary Key #10

Open
silverark opened this issue Jan 25, 2021 · 1 comment
Open

Composite Primary Key #10

silverark opened this issue Jan 25, 2021 · 1 comment

Comments

@silverark
Copy link

If I need a primary key with multiple columns is there a way of doing this with your library? I tried excluding the primary key so I could run an Alter Table command afterwards, but it seems I can't create a table without a primary key as I get this error:

2021/01/25 21:19:25 Migrating CreateUserRolesTable
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x4f6256]

goroutine 1 [running]:
github.com/ShkrutDenis/go-migrations/builder/mysql/key.(*PrimaryKey).GetSQL(0x0, 0x76ce00, 0xc000160800)                                                                          
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/builder/mysql/key/primary.go:33 +0x26                                                                           
github.com/ShkrutDenis/go-migrations/builder/mysql/table.(*Table).createTableSQL(0xc000160600, 0x20, 0x6eb1e0)                                                                    
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/builder/mysql/table/table.go:158 +0x299                                                                         
github.com/ShkrutDenis/go-migrations/builder/mysql/table.(*Table).GetSQL(0xc000160600, 0x2, 0x10)                                                                                 
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/builder/mysql/table/table.go:133 +0x15f                                                                         
github.com/ShkrutDenis/go-migrations/builder/mysql/table.(*Table).MustExec(0xc000160600)
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/builder/mysql/table/table.go:246 +0x40                                                                          
purplevisits.com/mdm/migrations/list.(*CreateUserRolesTable).Up(0x93e9f8, 0xc000089e30)
        /app/migrations/list/1_create_user_roles_table.go:18 +0x13d
github.com/ShkrutDenis/go-migrations.upOrIgnore(0x7689c0, 0x93e9f8)
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/entry.go:50 +0xea
github.com/ShkrutDenis/go-migrations.Run(0xc0000761e0, 0x6, 0x6)
        /go/pkg/mod/github.com/!shkrut!denis/go-migrations@v1.2.1/entry.go:27 +0x86

What I'm trying to create is this:

CREATE TABLE user_roles(
   user_id INT,
   role_id INT,
   PRIMARY KEY(user_id,role_id)
)

The above error was generated with this code. It threw the warning before I called come ALTER TABLE bit.

table := mysql.NewTable("user_roles", con)
	table.Column("user_id").Type("int").NotNull()
	table.Column("role_id").Type("int").NotNull()
	table.MustExec()
	_, _ = con.DB.Exec("ALTER TABLE user_roles ADD PRIMARY KEY(user_id,role_id);")

Thanks for your help. Enjoying the library so far!

@ShkrutDenis
Copy link
Owner

Hello
Sorry, but currently this lib hasn't a posibility to create composite Primary Key by query builder.

Generally i adding functionality to this lib by my needs, but now i see not only me want to use this lib, so i will add posibility to create composite Primary Key. And let you know, when i add it.

Thank you for your intreseting fot this lib and for the opening this issue.

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

No branches or pull requests

2 participants