D1.exec is not a function

This works locally with wrangler so this is only a problem after being deployed.

I can run a db.prepare statement and it works, but right after a prepare, I tried an exec to create a table and it gives me a db.exec is not a function error.

Part of the log showing this behavior:

{
      "message": [
        "ERROR:",
        "Error: D1_ERROR: no such table: migrations"
      ],
      "level": "error",
      "timestamp": 1710872546172
    },
    {
      "message": [
        "MIGRATION:",
        0,
        -1
      ],
      "level": "log",
      "timestamp": 1710872546172
    },
    {
      "message": [
        "RUNNING MIGRATION: ",
        0,
        "CREATE TABLE IF NOT EXISTS migrations (id integer PRIMARY KEY, createdAt text)"
      ],
      "level": "log",
      "timestamp": 1710872546172
    },
    {
      "message": [
        "ERROR:",
        "TypeError: db.exec is not a function"
      ],

Is exec disabled in production or something?

Hi @treeder

These are the available functions in D1/wrangler: Commands - Wrangler · Cloudflare Workers docs

That would make me think I could use exec in a worker, which I can’t so I don’t think those are the available commands within a worker.

exec should work fine according to the docs:

Can you share a full code snippet and reproduction?

export default {
    async fetch(request, env, ctx) {
          env.D1.exec('CREATE TABLE IF NOT EXISTS users (id text PRIMARY KEY, createdAt text, updatedAt text, name text, email text, image text )`)
   }
}