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

Problem migrate from PostgreSQL to MySQL #16

Closed
tangnotes opened this issue Oct 9, 2016 · 3 comments
Closed

Problem migrate from PostgreSQL to MySQL #16

tangnotes opened this issue Oct 9, 2016 · 3 comments

Comments

@tangnotes
Copy link

When I was trying to migrate from PostgreSQL to MySQL, it took more effort than I thought. Due to the inconsistent data type of IDs, when gorm try to create foreign key, following error occurs:

Error 1215: Cannot add foreign key constraint

gorm defines the ID with unit type, which maps to MySQL int(10) unsigned:

package gorm

import "time"

type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

But ClientID, UserID defined as sql.NullInt64, which map to MySQL bigint(20):

type RefreshToken struct {
    gorm.Model
    ClientID  sql.NullInt64 `sql:"index;not null"`
    UserID    sql.NullInt64 `sql:"index"`
    Client    *Client
    User      *User
    Token     string    `sql:"type:varchar(40);unique;not null"`
    ExpiresAt time.Time `sql:"not null"`
    Scope     string    `sql:"type:varchar(200);not null"`
}
@RichardKnop
Copy link
Owner

RichardKnop commented Dec 4, 2016

@fwso Yes I agree. I should have just used int64 everywhere. Not sure why I used uint for primary keys. This should be refactored. Feel free to submit your changes as PR!

Perhaps redefine the gorm model to use int64. Btw, I have decoupled all models into a separate package. So the updated gorm model could go to models/common.go.

@kira8565
Copy link

kira8565 commented Dec 6, 2016

I change the sql.NullInt64 to uint and migrate to mysql,every thing looks fine......But unit tests may need to be adjusted.....

@adam-hanna
Copy link
Contributor

Does this fork help? I changed all ID's to uuids generated in go.

https://github.com/adam-hanna/go-oauth2-server/tree/feature/uuid

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

4 participants