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

Unsigned int issues #201

Closed
pjebs opened this Issue Aug 16, 2014 · 8 comments

Comments

Projects
None yet
2 participants
@pjebs
Copy link

pjebs commented Aug 16, 2014

Making a field type unsigned doesn't seem to work.

I've tried:
Key uint32

and
Key uint32 'sql:"type:unsigned"'

Both seem to just make it normal signed in in the mysql database

@jinzhu

This comment has been minimized.

Copy link
Owner

jinzhu commented Aug 16, 2014

It should be:

package main

import (
    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
)

type Test struct {
    Id  int64
    Key uint32 `sql:"type:int unsigned"`
}

func main() {
    db, _ := gorm.Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
    db.Debug().AutoMigrate(&Test{})
}

But seems better to include unsigned for uint by default?

@pjebs

This comment has been minimized.

Copy link
Author

pjebs commented Aug 16, 2014

It would be better if it was unsigned by default.

@pjebs

This comment has been minimized.

Copy link
Author

pjebs commented Aug 16, 2014

There is now another problem:
Originally (before your suggested change):
type Customer struct {
Id uint32
Name string sql:"size:255"
Key uint64 sql:"unique"
Secret string sql:"size:32" //md5 returns 32 character hex string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
}
//Fill in new customer
var customer Customer
db.FirstOrCreate(&customer, Customer{Name: "test_name"})
db.Save(&customer)

This use to work perfectly.

Now I changed primary key to unsigned:

type Customer struct {
Id uint32 sql:"type:int unsigned"
Name string sql:"size:255"
Key uint64 sql:"unique" //2147483647 (issue with unsigned variable)
Secret string sql:"size:32" //md5 returns 32 character hex string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
}

Now the same code doesn't automatically fill in the primary_key id anymore (in the actual database)

@pjebs

This comment has been minimized.

Copy link
Author

pjebs commented Aug 16, 2014

Perhaps I need to use this:
gorm:"primary_key:yes"

But how do I include it when I already have sql:"type:int unsigned" there already

@pjebs

This comment has been minimized.

Copy link
Author

pjebs commented Aug 16, 2014

(i.e it loses primary key status and also no longer auto-increments)

@jinzhu

This comment has been minimized.

Copy link
Owner

jinzhu commented Aug 19, 2014

Yes, @pjebs , when you set sql type, you need to include all types.

For this example, it's primary key, so you need to include AUTO_INCREMENT, so the type should looks like

sql:"type:int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY"

Although it looks really long, but it is necessary ;(

@jinzhu jinzhu closed this Aug 19, 2014

@pjebs

This comment has been minimized.

Copy link
Author

pjebs commented Aug 19, 2014

Would the query function which fetches the data using primary key still work after I make these changes manually?

@jinzhu

This comment has been minimized.

Copy link
Owner

jinzhu commented Aug 19, 2014

Yes, this will works as the primary key named as Id

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