Skip to content

Commit

Permalink
fixes markbates/pop#63, allowing int64 IDs on models
Browse files Browse the repository at this point in the history
  • Loading branch information
natedsaint authored and markbates committed Apr 19, 2017
1 parent 93fcbfb commit f51bc21
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type dialect interface {
func genericCreate(s store, model *Model, cols Columns) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int":
case "int", "int64":
var id int64
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
Expand All @@ -48,7 +48,12 @@ func genericCreate(s store, model *Model, cols Columns) error {
}
id, err = res.LastInsertId()
if err == nil {
model.setID(int(id))
if keyType == "int" {
model.setID(int(id))
}
if keyType == "int64" {
model.setID(int64(id))
}
}
return errors.Wrap(err, "couldn't get the last inserted id")
case "UUID":
Expand Down
2 changes: 2 additions & 0 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (q *Query) Find(model interface{}, id interface{}) error {
switch t := id.(type) {
case int:
idi = t
case int64:
idi = int(t)
case uuid.UUID:
return q.Where(idq, t.String()).First(model)
case string:
Expand Down
5 changes: 4 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (m *Model) validateUpdate(c *Connection) (*validate.Errors, error) {
}

// ID returns the ID of the Model. All models must have an `ID` field this is
// of type `int` or of type `uuid.UUID`.
// of type `int`,`int64` or of type `uuid.UUID`.
func (m *Model) ID() interface{} {
fbn, err := m.fieldByName("ID")
if err != nil {
Expand Down Expand Up @@ -182,5 +182,8 @@ func (m *Model) whereID() string {
if _, ok := id.(int); ok {
return fmt.Sprintf("%s.id = %d", m.TableName(), id)
}
if _, ok := id.(int64); ok {
return fmt.Sprintf("%s.id = %d", m.TableName(), id)
}
return fmt.Sprintf("%s.id ='%s'", m.TableName(), id)
}

0 comments on commit f51bc21

Please sign in to comment.