lapis
lapis copied to clipboard
Use last field of a compound PK when setting auto ids
In complex models with multi-field primary keys, the auto_increment field is last. Without this, @primary_key is set into values directly (key is a table), like values[{"user_id", "id"}] = 123. While id in this example is unique regardless of user_id, it's reasonable to include user_id in the primary key to better locate the user's data.
Think you can add a test for this or do you want me to? I'm not familiar enough with mysql for this case so I would have to verify with a test
I will add a test.
I've spent the last 4 hours trying to get all the prerequisites installed such that I can run "busted spec_mysql" and will have to continue tomorrow. A quick test might be to change the spec_mysql/models.moon class Posts so that user_id is before id, and add "PRIMARY KEY (user_id, id)", and then add a test that verifies that id still gets incremented in the model returned from create. I haven't verified that, yet, but will continue working to get my environment set up to do so.
Had a couple false starts, but this one looks good. I considered modifying Posts as I previously suggested, but too much other test code would have to change to support it. I found it cleaner to just add a new model, for testing the new functionality.
Hey @leafo, have you had a chance to look at the diff? Just checking in, thanks.