-
-
Notifications
You must be signed in to change notification settings - Fork 246
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
fix: replace "id" with model.IDField #604
Changes from 4 commits
e25179b
68bb93d
6965b00
91a5f8e
15e0292
c4b8e6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,7 +228,7 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error { | |
} | ||
|
||
tn := m.TableName() | ||
cols := columns.ForStructWithAlias(m.Value, tn, m.As) | ||
cols := m.Columns() | ||
|
||
if tn == sm.TableName() { | ||
cols.Remove(excludeColumns...) | ||
|
@@ -350,8 +350,8 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error { | |
} | ||
|
||
tn := m.TableName() | ||
cols := columns.ForStructWithAlias(model, tn, m.As) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This uses the outer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think this has had problems in the past where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like it actually does not matter. It is not possible to pass a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know too little about reflection to judge this statement, so I would prefer reverting this to the original behavior just in case this breaks something somewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, reverted |
||
cols.Remove("id", "created_at") | ||
cols := m.Columns() | ||
cols.Remove(m.IDField(), "created_at") | ||
|
||
if tn == sm.TableName() { | ||
cols.Remove(excludeColumns...) | ||
|
@@ -393,11 +393,11 @@ func (c *Connection) UpdateColumns(model interface{}, columnNames ...string) err | |
|
||
cols := columns.Columns{} | ||
if len(columnNames) > 0 && tn == sm.TableName() { | ||
cols = columns.NewColumnsWithAlias(tn, m.As) | ||
cols = columns.NewColumnsWithAlias(tn, m.As, sm.IDField()) | ||
cols.Add(columnNames...) | ||
|
||
} else { | ||
cols = columns.ForStructWithAlias(model, tn, m.As) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #604 (comment) |
||
cols = m.Columns() | ||
} | ||
cols.Remove("id", "created_at") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -510,6 +510,25 @@ func Test_Create_With_Non_ID_PK_String(t *testing.T) { | |
}) | ||
} | ||
|
||
func Test_Create_Non_PK_ID(t *testing.T) { | ||
if PDB == nil { | ||
t.Skip("skipping integration tests") | ||
} | ||
transaction(func(tx *Connection) { | ||
r := require.New(t) | ||
|
||
count, err := tx.Count(&HydraClient{}) | ||
client := &HydraClient{ | ||
OutfacingID: "a client of hydra", | ||
} | ||
r.NoError(tx.Create(client)) | ||
|
||
ctx, err := tx.Count(&HydraClient{}) | ||
r.NoError(err) | ||
r.Equal(count+1, ctx) | ||
zepatrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
func Test_Eager_Create_Has_Many(t *testing.T) { | ||
if PDB == nil { | ||
t.Skip("skipping integration tests") | ||
|
@@ -1470,6 +1489,44 @@ func Test_Update_UUID(t *testing.T) { | |
}) | ||
} | ||
|
||
func Test_Update_With_Non_ID_PK(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, |
||
if PDB == nil { | ||
t.Skip("skipping integration tests") | ||
} | ||
transaction(func(tx *Connection) { | ||
r := require.New(t) | ||
|
||
cc := CrookedColour{ | ||
Name: "You?", | ||
} | ||
err := tx.Create(&cc) | ||
r.NoError(err) | ||
|
||
cc.Name = "Me!" | ||
r.NoError(tx.Update(&cc)) | ||
zepatrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
func Test_Update_Non_PK_ID(t *testing.T) { | ||
if PDB == nil { | ||
t.Skip("skipping integration tests") | ||
} | ||
transaction(func(tx *Connection) { | ||
r := require.New(t) | ||
|
||
client := &HydraClient{ | ||
OutfacingID: "my awesome hydra client", | ||
} | ||
r.NoError(tx.Create(client)) | ||
|
||
updatedID := "your awesome hydra client" | ||
client.OutfacingID = updatedID | ||
r.NoError(tx.Update(client)) | ||
r.NoError(tx.Reload(client)) | ||
r.Equal(updatedID, client.OutfacingID) | ||
zepatrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
func Test_Destroy(t *testing.T) { | ||
if PDB == nil { | ||
t.Skip("skipping integration tests") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package pop | |
|
||
import ( | ||
"fmt" | ||
"github.com/gobuffalo/pop/v5/columns" | ||
"github.com/pkg/errors" | ||
"reflect" | ||
"sync" | ||
|
@@ -46,7 +47,18 @@ func (m *Model) ID() interface{} { | |
// IDField returns the name of the DB field used for the ID. | ||
// By default, it will return "id". | ||
func (m *Model) IDField() string { | ||
field, ok := reflect.TypeOf(m.Value).Elem().FieldByName("ID") | ||
modelType := reflect.TypeOf(m.Value) | ||
|
||
// remove all indirections | ||
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr || modelType.Kind() == reflect.Array { | ||
modelType = modelType.Elem() | ||
} | ||
|
||
if modelType.Kind() == reflect.String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can model be a string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there are several test cases for that, e.g. https://github.com/gobuffalo/pop/blob/master/executors_test.go#L1506 |
||
return "id" | ||
} | ||
|
||
field, ok := modelType.FieldByName("ID") | ||
if !ok { | ||
return "id" | ||
} | ||
|
@@ -101,6 +113,10 @@ func (m *Model) TableName() string { | |
return tableMap[cacheKey] | ||
} | ||
|
||
func (m *Model) Columns() columns.Columns { | ||
return columns.ForStructWithAlias(m.Value, m.TableName(), m.As, m.IDField()) | ||
} | ||
|
||
func (m *Model) cacheKey(t reflect.Type) string { | ||
return t.PkgPath() + "." + t.Name() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
drop_table("hydra_clients") | ||
zepatrik marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
create_table("hydra_clients") { | ||
zepatrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Column("pk", "int", { primary: true }) | ||
t.Column("id", "string", {}) | ||
|
||
t.DisableTimestamps() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is missing two tests:
id
and the ID field can not be written but can be read.notid
and the ID field ca be written and read butnotid
can not be written.have a test where the PK is not
"id"
and you are not able to update that particular PKThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added cases but not everything is possible like you expect, see #556