cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.6k stars 674 forks source link

🐛 BUG: D1 - Create trigger example works on local D1, but not cloud D1 #4998

Open evoshawkins opened 7 months ago

evoshawkins commented 7 months ago

Which Cloudflare product(s) does this pertain to?

D1

What version(s) of the tool(s) are you using?

3.28.1 [Wrangler]

What version of Node are you using?

v18.17.0

What operating system and version are you using?

Windows 10 Enterprise

Describe the Bug

Observed behavior

The attached SQL works without error on local D1 wrangler d1 execute gateway --local --file=./developer.txt

But fails with error 'incomplete input [code: 7500]' on cloud D1. wrangler d1 execute gateway --file=./developer.txt

After removing the trigger clause and executing again, the error is gone and the command completes.

Expected behavior

The SQL should work without error on the cloud/non-local D1 database.

developer.txt

Steps to reproduce

Having a workers project with a D1 binding, only the wrangler CLI is need to reproduce.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

[ERROR] A request to the Cloudflare API (/accounts/19d8630770d1a38b0ed6e6d0f6fa54b9/d1/database/63dfffdd-1c19-412e-be20-6a789d4f6c71/query) failed.

incomplete input [code: 7500]

rafaelsg-01 commented 7 months ago

I'm having exactly the same issue here, any solution?

rafaelsg-01 commented 7 months ago

Sorry for my English. I managed to find a very simple solution to work around the bug. I executed the command --file="./migration.sql" to create the tables normally, but to create the trigger I used the command --command="CODE_SQL".

The final result looks like this:

wrangler d1 execute name_d1 --file=./migration.sql
wrangler d1 execute name_d1 --command="CREATE TRIGGER name_trigger AFTER UPDATE ON name_table BEGIN UPDATE name_table SET updated_at = CURRENT_TIMESTAMP WHERE user_id = NEW.user_id; END;"

This way it worked as expected. In the future, they should fix this bug. For now, this is the only solution I found.

evoshawkins commented 7 months ago

I can confirm that your work-around works. Well done! @rafaelsg-01

There is, of course, still a bug for CF to fix here.

bricej13 commented 5 months ago

Another work-around that works: put the code for the trigger in your migration file on a single line.

❌ Before:

CREATE TRIGGER IF NOT EXISTS food_ai
    AFTER INSERT
    ON food
BEGIN
    INSERT INTO food_search(rowid, brand, flavor) VALUES (new.id, new.brand, new.flavor);
END;

✅ After:

CREATE TRIGGER IF NOT EXISTS food_ai AFTER INSERT ON food BEGIN INSERT INTO food_search(rowid, brand, flavor) VALUES (new.id, new.brand, new.flavor); END;