fix cleanup using auto-generated primary key; add tests.