Skip to content

[BUG]: using placeholders for inserts in sqlite doesn't work for types that transform values #976

@gmaclennan

Description

@gmaclennan

What version of drizzle-orm are you using?

0.27.2

What version of drizzle-kit are you using?

0.19.12

Describe the Bug

If I try to use a placeholder to insert values for a custom type, the value inserted is incorrect.

The same custom type works fine when used in a (non-prepared) insert.

e.g. given this schema:

import { integer, sqliteTable, customType } from 'drizzle-orm/sqlite-core'

const textArray = customType({
    dataType() { return 'text' },
    fromDriver(value) { return JSON.parse(value) },
    toDriver(value) { return JSON.stringify(value) },
})

export const table = sqliteTable('table', {
  list: textArray('list')
})

The following returns { list: { name: 'list' } } instead of { list: [] }:

const stmt = db
  .insert(table)
  .values({ list: placeholder('list') })
  .prepare()

stmt.run({ list: [] })
console.log(db.select().from(table).get())

However a normal insert (without prepare and placeholders) works fine:

db.insert(table).values({ list: [] }).run()
console.log(db.select().from(table).get())
// works as expected
// { list: [] }

What I can see is happening is that when valued() is called, the toDriver() function of my customType is called with a Placeholder instance. JSON.stringify() is being called on the Placeholder instance, which returns { name: 'list' }, which is what is being written to the database.

Minimal reproduction code: https://github.com/gmaclennan/drizzle-placeholder-bug/blob/main/index.js

Expected behavior

I would expect placeholders to work with custom fields in sqlite

Environment & setup

Running on MacOS, M2, Node v16.20.0, using better-sqlite3 as the driver.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions