Custom vector type with pgvector

I'm trying to use the pgvector extension with a custom type but am running into an issue with the migration file. My custom type looks like
export const vector = customType<
{
data: number[];
driverData: string;
config: { dimension: number };
default: false
}
>({
dataType(config) {
return `vector(${config?.dimension ?? 3})`;
},
toDriver(value: number[]): string {
return JSON.stringify(value)
},
fromDriver(value: string): number[] {
return JSON.parse(value);
},
});

export const myTable = pgTable('my_table', {
embedding: vector('embedding', { dimension: 3 })
})
export const vector = customType<
{
data: number[];
driverData: string;
config: { dimension: number };
default: false
}
>({
dataType(config) {
return `vector(${config?.dimension ?? 3})`;
},
toDriver(value: number[]): string {
return JSON.stringify(value)
},
fromDriver(value: string): number[] {
return JSON.parse(value);
},
});

export const myTable = pgTable('my_table', {
embedding: vector('embedding', { dimension: 3 })
})
However when adding a vector column my generated migration file looks like
ALTER TABLE "my_table" ADD COLUMN "embedding" "vector(3)";
ALTER TABLE "my_table" ADD COLUMN "embedding" "vector(3)";
Notice the quotes around "vector(3)". Postgres is interpreting this as the literal type "vector(3)" instead of vector from pgvector. Applying the migration gives me a the following error:PostgresError: type "vector(3)" does not exist I confirmed that removing the quotes fixes the issue. Is there a way to tell the custom type to not wrap quotes here?
4 Replies
PGT
PGT15mo ago
@fjorn did you ever find a solution for this?
fjorn
fjorn15mo ago
Sort of. It actually does work for a migration that declares the table instead of altering it by adding a column. The work around works for me since my project is early enough to restart my migrations to include vectors on the original table definition. This may have to be submitted as an actual bug github issue on the drizzle-kit mirror though if it's still happening for table altering migrations
PGT
PGT15mo ago
oh I see, so basically if it's a new table, you're good, otherwise you'll have to manually edit the SQL file to remove the quotes
fjorn
fjorn15mo ago
that's been my experience yes