Error: D1_ERROR: too many SQL variables at offset 460: SQLITE_ERROR
Hey there! Getting the following error
Error: D1_ERROR: too many SQL variables at offset 460: SQLITE_ERROR
at D1DatabaseSessionAlwaysPrimary._sendOrThrow (cloudflare-internal:d1-api:129:19)
at async D1PreparedStatement.run (cloudflare-internal:d1-api:308:29)
... 4 lines matching cause stack trace ...
at async drainBody (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10) {
[cause]: Error: too many SQL variables at offset 460: SQLITE_ERROR
at D1DatabaseSessionAlwaysPrimary._sendOrThrow (cloudflare-internal:d1-api:130:24)
at async D1PreparedStatement.run (cloudflare-internal:d1-api:308:29)
at async Object.handler (file:///Users/haris/code/posthog-cohorts-to-cio-segments/src/schedules/fetchCohorts.ts:282:6)
at async Promise.all (index 0)
at async Object.scheduled (file:///Users/haris/code/posthog-cohorts-to-cio-segments/src/index.ts:11:3)
at async scheduled (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-scheduled.ts:8:3)
at async drainBody (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)
}
Error: D1_ERROR: too many SQL variables at offset 460: SQLITE_ERROR
at D1DatabaseSessionAlwaysPrimary._sendOrThrow (cloudflare-internal:d1-api:129:19)
at async D1PreparedStatement.run (cloudflare-internal:d1-api:308:29)
... 4 lines matching cause stack trace ...
at async drainBody (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10) {
[cause]: Error: too many SQL variables at offset 460: SQLITE_ERROR
at D1DatabaseSessionAlwaysPrimary._sendOrThrow (cloudflare-internal:d1-api:130:24)
at async D1PreparedStatement.run (cloudflare-internal:d1-api:308:29)
at async Object.handler (file:///Users/haris/code/posthog-cohorts-to-cio-segments/src/schedules/fetchCohorts.ts:282:6)
at async Promise.all (index 0)
at async Object.scheduled (file:///Users/haris/code/posthog-cohorts-to-cio-segments/src/index.ts:11:3)
at async scheduled (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-scheduled.ts:8:3)
at async drainBody (file:///Users/haris/Library/pnpm/global/5/.pnpm/[email protected]/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)
}
1 Reply
const chunkSize = 1000;
for (let i = 0; i < Math.ceil(personsToAddToSegment.length / chunkSize); i++) {
const chunk = personsToAddToSegment.slice(i * chunkSize, (i + 1) * chunkSize);
const addResponse = await fetch(
`https://track.customer.io/api/v1/segments/${cohort.segmentId}/add_customers?id_type=email`,
{
method: "POST",
headers: {
Authorization: `Basic ${Buffer.from(`${environment.CUSTOMER_IO_SITE_ID}:${environment.CUSTOMER_IO_TRACK_API_KEY}`).toString("base64")}`,
},
body: JSON.stringify({
ids: chunk.map((person) => person.properties.email),
}),
},
);
if (!addResponse.ok) {
const text = await addResponse.text();
console.log("Failed to add persons to segment", text);
continue;
}
console.log(`Added ${chunk.length} persons to segment for cohort ${cohort.cohortId}.`);
try {
await db.insert(schema.cohortPersonsTable).values(
chunk.map((person) => ({
cohortId: cohort.cohortId,
userId: person.uuid,
})),
);
} catch (error) {
console.log("Failed to add persons to cohort persons in db", error);
}
}
const chunkSize = 1000;
for (let i = 0; i < Math.ceil(personsToAddToSegment.length / chunkSize); i++) {
const chunk = personsToAddToSegment.slice(i * chunkSize, (i + 1) * chunkSize);
const addResponse = await fetch(
`https://track.customer.io/api/v1/segments/${cohort.segmentId}/add_customers?id_type=email`,
{
method: "POST",
headers: {
Authorization: `Basic ${Buffer.from(`${environment.CUSTOMER_IO_SITE_ID}:${environment.CUSTOMER_IO_TRACK_API_KEY}`).toString("base64")}`,
},
body: JSON.stringify({
ids: chunk.map((person) => person.properties.email),
}),
},
);
if (!addResponse.ok) {
const text = await addResponse.text();
console.log("Failed to add persons to segment", text);
continue;
}
console.log(`Added ${chunk.length} persons to segment for cohort ${cohort.cohortId}.`);
try {
await db.insert(schema.cohortPersonsTable).values(
chunk.map((person) => ({
cohortId: cohort.cohortId,
userId: person.uuid,
})),
);
} catch (error) {
console.log("Failed to add persons to cohort persons in db", error);
}
}