You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
@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.
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:gorm
defines the ID withunit
type, which maps to MySQLint(10) unsigned
:But
ClientID
,UserID
defined assql.NullInt64
, which map to MySQLbigint(20)
:The text was updated successfully, but these errors were encountered: