lapis icon indicating copy to clipboard operation
lapis copied to clipboard

Use last field of a compound PK when setting auto ids

Open gheckenbach opened this issue 5 years ago • 5 comments

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.

gheckenbach avatar Apr 30 '20 20:04 gheckenbach

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

leafo avatar Apr 30 '20 20:04 leafo

I will add a test.

gheckenbach avatar Apr 30 '20 21:04 gheckenbach

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.

gheckenbach avatar May 01 '20 01:05 gheckenbach

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.

gheckenbach avatar May 05 '20 18:05 gheckenbach

Hey @leafo, have you had a chance to look at the diff? Just checking in, thanks.

gheckenbach avatar May 18 '20 17:05 gheckenbach