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

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

Open evoshawkins opened this issue 1 year ago • 3 comments

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.

  • Download the attached developer.txt file
  • Run the command: wrangler d1 execute gateway --local --file=./developer.txt
  • Observe that there is no error
  • Run the command: wrangler d1 execute gateway --file=./developer.txt
  • Observe the code: 7500 error

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]

evoshawkins avatar Feb 12 '24 20:02 evoshawkins

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

rafaelsg-01 avatar Feb 13 '24 10:02 rafaelsg-01

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.

rafaelsg-01 avatar Feb 13 '24 12:02 rafaelsg-01

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

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

evoshawkins avatar Feb 20 '24 21:02 evoshawkins

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;

bricej13 avatar Apr 12 '24 14:04 bricej13