yesodweb / yesodweb/persistent

MySQL insert is broken on non-auto incrementing primary keys

Open
#1,251 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

MySQL can't do RETURNING (tho Maria DB can), which means that our insert needs to pair it with a SELECT LAST_INSERT_ID().

Of course, LAST_INSERT_ID() only works with auto incrementing primary keys. It fails utterly with (as an example) the UUID type.

The general recommended fix is to write something like:

CREATE TABLE foo (
  id  VARCHAR(80) DEFAULT (UUID()) PRIMARY KEY,
  name VARCUAR (80) NOT NULL
);

INSERT INTO foo (id, name) VALUES (@last_foo_id = UUID(), ?);
SELECT @last_foo_id;

which is, y'know, horrible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by locating the MySQL insert implementation and its LAST_INSERT_ID handling, then reproduce the failure with a UUID or other non-auto-incrementing primary key. Done means inserts work for such keys without breaking auto-increment behavior, with regression coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, mysql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.