Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

bug(d1): too many parameters when preparing a D1 statement #504

Closed
4x4notfound opened this issue Feb 16, 2023 · 4 comments · Fixed by #544
Closed

bug(d1): too many parameters when preparing a D1 statement #504

4x4notfound opened this issue Feb 16, 2023 · 4 comments · Fixed by #544
Labels
bug Something isn't working

Comments

@4x4notfound
Copy link

4x4notfound commented Feb 16, 2023

just upgraded to wrangler 2.10.0 and getting new D1_ERROR which has the new miniflare version

[cause]: Error: Error: RangeError: Too many parameter values were provided at D1Database._send (node_modules/@miniflare/d1/src/d1js.ts:149:16)

I'm only trying to bind 4 values. Example

db
      .prepare(
        'INSERT INTO node (urn, nid, nss, fragment) VALUES (?1, ?2, ?3, ?4) \
        ON CONFLICT(urn) DO UPDATE SET fragment = excluded.fragment'
      )
      .bind(id, nid, nss, fc)
      .run()

Note this runs fine in CF workers.

@4x4notfound
Copy link
Author

4x4notfound commented Feb 16, 2023

looks like bind create a new D1PreparedStatement that sets this.params and on the fetch on the binding using _send doesn't like the params.

const body = JSON.stringify(

My guess is this is where the error happens? But this is just better-sqlite?

stmt.run(params);

@4x4notfound
Copy link
Author

4x4notfound commented Feb 17, 2023

using ?, ?, ?, ? instead of ?1, ?2, ?3, ?4 should work. It looks like better-sqlite3 wants params of the form [a,b,c,d] for ?, ?, ?, ? but {1: a, 2: b, 3: c, 4: d} for ?1, ?2, ?3, ?4

Hmmm, looks like there are some open issues/PRs on better-sqlite3 for this:
WiseLibs/better-sqlite3#576
WiseLibs/better-sqlite3#725

@subhendukundu
Copy link

Facing the same issue. :(

@mrbbot mrbbot added the bug Something isn't working label Mar 21, 2023
mrbbot added a commit that referenced this issue Mar 22, 2023
`better-sqlite3` expects parameters of the form `?1, ?2, ...` to be
bound as an object of the form `{ 1: params[0], 2: params[1], ...}`.
In #480, we accidentally removed the code that handled this case.
This PR adds it back, and lifts out some common functionality into a
`#prepareAndBind()` function. :)

Thanks @ruslantalpa for spotting the removed code.

Closes #526
Closes cloudflare/workers-sdk#2811
Closes cloudflare/workers-sdk#2887
mrbbot added a commit that referenced this issue Mar 23, 2023
…544)

`better-sqlite3` expects parameters of the form `?1, ?2, ...` to be
bound as an object of the form `{ 1: params[0], 2: params[1], ...}`.
In #480, we accidentally removed the code that handled this case.
This PR adds it back, and lifts out some common functionality into a
`#prepareAndBind()` function. :)

Thanks @ruslantalpa for spotting the removed code.

Closes #526
Closes cloudflare/workers-sdk#2811
Closes cloudflare/workers-sdk#2887
@michaelcpuckett
Copy link

Hi is it possible this issue is not resolved yet?

        const keys = Object.keys(convertedEntity);
        const valuesPlaceholders = keys.map((_, index) => `?${index + 1}`).join(', ');
        const values = Object.values(convertedEntity);
        const stmt = this.db.prepare(`INSERT INTO ${collectionName} (${keys.map(key => `"${key}"`).join(', ')}) VALUES (${valuesPlaceholders});`).bind(...values);
        await stmt.run();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants