workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

๐Ÿš€ Feature Request: D1 Transaction support

Open codehz opened this issue 3 years ago โ€ข 42 comments

Describe the solution

Has the procedures feature mentioned in this article been shipped? This feature does not seem to be available in the current version sdk, either locally or remotely. Is there another plan for this? I really want this feature.

One of my use cases is to cascade insert data into two (or more) tables, where the generated id obtained by the first insert statement is used as a parameter in the second insert statement. These two inserts need to be done in the same transaction, otherwise the inconsistent state may cause some problem. (In other database engines, such as postgresql, we can achieve this through subqueries or cte with returning clauses in one statement. Unfortunately, this method does not work in sqlite)

codehz avatar Feb 15 '23 16:02 codehz

Hey @codehz - it hasn't been shipped yet, though @geelen will be working on it soon

rozenmd avatar Feb 15 '23 17:02 rozenmd

Yeah afaik there's no way to do an insert in a subquery, but you can use last_insert_rowid() inside a batch call to get a bit of what you're after. I just confirmed this works in both native SQLite and D1:

PRAGMA foreign_keys=on;

drop table if exists b;
drop table if exists a;

create table a (
   id INTEGER PRIMARY KEY,
   value TEXT
);

create table b (
   id INTEGER PRIMARY KEY,
   a_id INTEGER,
   value TEXT,
   FOREIGN KEY (a_id) REFERENCES a(id)
);

INSERT INTO a (value) VALUES ('foo');
INSERT INTO b (a_id, value) VALUES (last_insert_rowid(), 'bar');

INSERT INTO a (value) VALUES ('baz');
INSERT INTO b (a_id, value) VALUES (last_insert_rowid(), 'boo');
image

Transactions are coming but hopefully this can unblock you in the meantime?

geelen avatar Feb 15 '23 20:02 geelen

It does work in this scenario, but in fact what I'm creating a generic framework that doesn't assume that users use rowid(or its alias) as a constraint for foreign key references. Indeed, there is still another workaround to use the previous rowid in the subquery to query the referenced key. This is exactly what I'm trying to do now. it will looks like this:

create table A(id default (lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))), value);
create table B(id default (lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))), aid, value);
insert into A(value) values (42);
insert into B(aid, value) values ((select id from A where A.rowid = last_insert_rowid()), 8);

But this method is still not robust enough, because users may create rowid-less tables for some optimization purposes. Or for more complex scenarios, create elements in more tables and then reference them in another table(This function only allows to get the previous rowid, and there is no way to store variables in a transaction).

codehz avatar Feb 16 '23 00:02 codehz

Yeah, it's definitely not a complete workaround, just a stopgap.

I'm creating a generic framework that doesn't assume that users use rowid(or its alias) as a constraint for foreign key references.

I think you're going to have to disallow rowid-less tables for the moment, then. Then when true JS/SQL procedures land, you can make things more generic.

geelen avatar Feb 16 '23 00:02 geelen

I think you're going to have to disallow rowid-less tables for the moment, then. Then when true JS/SQL procedures land, you can make things more generic.

I'm going to disable the whole feature (Insert an object along with its related objects through relationships) right now, since it require more complex solution if I need insert more than one related objects (last_insert_rowid can only get last one. This means that it may be necessary to create a temporary table to store the rowids of these related objects. The benefits it brings are not worth compared to the cost of increased complexity, not to mention that it will soon be replaced by a new mechanism), and it's not a very useful feature either, just for completeness(since hasura do).

codehz avatar Feb 16 '23 01:02 codehz

No worries. Hopefully this issue will help someone googling this problem in the short term. I'm going leave it open for now and update & close it once Transactions/Procedures lands.

geelen avatar Feb 16 '23 01:02 geelen

BTW, in this article, it is not clear which types we can pass to the Procedures 'worklet' (and return back). only primitive types? object which can be serialized to json (or structuredClone)? ArrayBuffer?

codehz avatar Feb 16 '23 01:02 codehz

Can't be sure at this stage, but I'm aiming for full structuredClone support. Worst case JSON-serialisable only.

If JSON is good enough though it might be easier to build so we might start there, ship that, then expand it later? Happy to hear suggestions on that front...

geelen avatar Feb 16 '23 02:02 geelen

JSON's hidden problem is, it cannot contains cyclic reference(and this might bring some surprises for user since it is transparent), other than that, the json looks good enough

codehz avatar Feb 16 '23 02:02 codehz

Any ETA on this feature?

OhSoooLucky avatar Oct 17 '23 06:10 OhSoooLucky

Any Updates on this feature?

AmaraFray avatar Jan 16 '24 17:01 AmaraFray

Hey @geelen - just checking in here - do we see transaction support being ready within the next couple of releases? ๐Ÿ˜

FlareLine avatar Jul 20 '24 06:07 FlareLine

+1๐Ÿ‘€๐Ÿ™Œ

takakikasuga avatar Oct 28 '24 01:10 takakikasuga

๐Ÿ‘€๐Ÿ†˜

duducpp avatar Nov 06 '24 21:11 duducpp

๐Ÿ‘€

richard-egeli avatar Nov 12 '24 15:11 richard-egeli

Feb 2023 to Dec 2024 and still nothing. its really a shame for D1 missing such an important feature!

mohamnag avatar Dec 31 '24 13:12 mohamnag

Would love an update on the roadmap of this item!

clgeoio avatar Jan 30 '25 18:01 clgeoio

This is a serious concern for us. Without transaction support, D1 is practically unsuitable for production OLTP usage

Ehbraheem avatar Feb 02 '25 02:02 Ehbraheem

Are there any updates? Or can we only use batch submission?

rxliuli avatar Feb 13 '25 02:02 rxliuli

We need this feature. Cant keep using Try and Catch exception, unreliable and blobby.

Nazvix avatar Feb 13 '25 05:02 Nazvix