coopernurse/gorp — pull request #95 by nelsam (2013-11-14 16:42:42+00:00)
Title: Autoincrement Bug With Embedded Structs

There are two bugs (related to a single problem) with autoincrement fields and embedded structs. If you do either of the following with stable gorp right now, things will go badly:

1) Use one of the embedded struct fields as an autoincrement field
2) Embed your struct before declaring your autoincrement field

Both of these issues happen because embedded fields all exist at the same field index, as far as reflect is concerned. However, the autoIncrIdx value is based on the column's index in the insert statement, which doesn't account for embedded fields all having the same index.

I've added a test for each of these cases to prove the problem, then changed the insert() function to use the FieldByName method. I'm not sure what the performance impact is (FieldByName can't be *significantly* slower, can it?), but it seems much better than the methods I was thinking of for continuing to use autoIncrIdx (e.g. looping through the fields and checking for embedded structs, then looping through embedded struct fields, until the indexes matched).