cloudflare / workers-sdk

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

🐛 BUG: Pages project with Astro getting D1_ERROR: no such table: #6122

Closed sz44 closed 1 week ago

sz44 commented 1 week ago

Which Cloudflare product(s) does this pertain to?

Pages

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

3.61.0 [Wrangler]

What version of Node are you using?

22.3.0

What operating system and version are you using?

Ubuntu 22.04

Describe the Bug

Observed behavior

[ERROR] Error: D1_ERROR: no such table: locations When trying to query remote D1 database from Astro project.

Expected behavior

Get remote D1 data from Astro project

Steps to reproduce

This is an Astro project created with: npm create cloudflare@latest my-astro-app -- --framework=astro A D1 Database was created with the name "staff-directory", it has a table called locations.

The table can be seen from the dashboard and also queried from commands such as: npx wrangler d1 execute --remote staff-directory --command="SELECT * FROM locations"

The code that throws the error is in index.astro:

const { env } = Astro.locals.runtime;

const ps = env.DB.prepare("SELECT * FROM locations"); const data = await ps.all(); console.log(data);

wrangler.toml: [[d1_databases]] binding = "DB" # i.e. available in your Worker on env.DB database_name = "staff-directory" database_id = "e7dce634-9248-407a-8611-c490028e753b"

To start the server: npm run preview In package.json it looks like: "preview": "astro build && wrangler pages dev",

Initially there was no error everything was working. The page at 127.0.0.1:8878 displayed the data from the database. It only started after I tried to bind to a different database. In the wrangler.toml file the database_name and database_id were changed to new values. Then I started seeing the D1_ERROR: no such table:. Later I restored wrangler.toml file to previous state but it's still giving the same error.

Please provide a link to a minimal reproduction

https://github.com/sz44/astro-cloudflare-test

Please provide any relevant error logs

No response

sz44 commented 1 week ago

I deployed the site and it worked fine so it looks like the issue was my misunderstanding of local vs remote development. The assumption was that if I run my site locally with the uuid and name of my D1 database that it will be able to reach it remotely, but it actually only talks to my local copy of the db.