{"url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554","repository_url":"https://api.github.com/repos/simonw/sqlite-utils","labels_url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554/labels{/name}","comments_url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554/comments","events_url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554/events","html_url":"https://github.com/simonw/sqlite-utils/issues/554","id":1720096994,"node_id":"I_kwDOCGYnMM5mhpji","number":554,"title":"`IndexError` when doing `.insert(..., pk='id')` after `insert_all`","user":{"login":"xavdid","id":1231935,"node_id":"MDQ6VXNlcjEyMzE5MzU=","avatar_url":"https://avatars.githubusercontent.com/u/1231935?v=4","gravatar_id":"","url":"https://api.github.com/users/xavdid","html_url":"https://github.com/xavdid","followers_url":"https://api.github.com/users/xavdid/followers","following_url":"https://api.github.com/users/xavdid/following{/other_user}","gists_url":"https://api.github.com/users/xavdid/gists{/gist_id}","starred_url":"https://api.github.com/users/xavdid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xavdid/subscriptions","organizations_url":"https://api.github.com/users/xavdid/orgs","repos_url":"https://api.github.com/users/xavdid/repos","events_url":"https://api.github.com/users/xavdid/events{/privacy}","received_events_url":"https://api.github.com/users/xavdid/received_events","type":"User","user_view_type":"public","site_admin":false},"labels":[],"state":"open","locked":false,"assignees":[],"milestone":null,"comments":2,"created_at":"2023-05-22T17:13:02Z","updated_at":"2025-11-13T05:10:47Z","closed_at":null,"assignee":null,"author_association":"NONE","active_lock_reason":null,"sub_issues_summary":{"total":0,"completed":0,"percent_completed":0},"issue_dependencies_summary":{"blocked_by":0,"total_blocked_by":0,"blocking":0,"total_blocking":0},"body":"I believe this is related to https://github.com/simonw/sqlite-utils/issues/98.\r\n\r\nWhen `pk` is specified by table A's `insert` call, it throws an index error if a different table has written a row with a higher rowid than exists in the first table. Here's a basic example:\r\n\r\n```py\r\nfrom sqlite_utils import Database\r\n\r\n\r\ndef test_pk_for_insert(fresh_db):\r\n    user = {\"id\": \"abc\", \"name\": \"david\"}\r\n\r\n    fresh_db[\"users\"].insert(user, pk=\"id\")\r\n\r\n    fresh_db[\"comments\"].insert_all(\r\n        [\r\n            {\"id\": \"def\", \"text\": \"ok\"},\r\n            {\"id\": \"ghi\", \"text\": \"great\"},\r\n        ],\r\n    )\r\n\r\n    fresh_db[\"users\"].insert(\r\n        user,\r\n        ignore=True,\r\n        # BUG: when specifying pk on the second insert call \r\n        # db.py goes into a block it doesn't expect and we get the error\r\n        pk=\"id\",\r\n    )\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    db = Database(\"bug.db\")\r\n    if db[\"users\"].exists():\r\n        raise ValueError(\r\n            \"bug only shows on a new database - remove bug.db before running the script\"\r\n        )\r\n    test_pk_for_insert(db)\r\n```\r\n\r\nThe error is:\r\n\r\n```py\r\n  File \"/Users/david/projects/reddit-to-sqlite/.venv/lib/python3.11/site-packages/sqlite_utils/db.py\", line 2960, in insert_chunk\r\n    row = list(self.rows_where(\"rowid = ?\", [self.last_rowid]))[0]\r\n          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^\r\nIndexError: list index out of range\r\n```\r\n\r\nThe issue is in this block: \r\n\r\nhttps://github.com/simonw/sqlite-utils/blob/2747257a3334d55e890b40ec58fada57ae8cfbfd/sqlite_utils/db.py#L2954-L2958\r\n\r\nrelevant locals are:\r\n\r\n- `pk`: `'id'`\r\n- `result.lastrowid`: `2`\r\n\r\nWhat's most interesting is the comment `# self.last_rowid will be 0 if a \"INSERT OR IGNORE\" happened`, which doesn't seem to be the case here. ","closed_by":null,"reactions":{"url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554/reactions","total_count":0,"+1":0,"-1":0,"laugh":0,"hooray":0,"confused":0,"heart":0,"rocket":0,"eyes":0},"timeline_url":"https://api.github.com/repos/simonw/sqlite-utils/issues/554/timeline","performed_via_github_app":null,"state_reason":null,"pinned_comment":null}