You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Modify driver.supportedDataTypes to support installed Postgres extension data type. Bypass validation for a column. #10056
I'm using the pgvector extension in my database, which allows me to store data in a vector datatype and perform vector operations such as nearest neighbors with SELECT * FROM items WHERE embedding <-> '[3,1,2]' < 5;. However, TypeORM seems to have an entity validator method that verifies whether the datatype defined in my entity exists.
For instance, the following won't work because the vector datatype doesn't natively exist.
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
Is there a way to modify or bypass the validation for a column? I could create this column using raw SQL, but that defeats the purpose of using an ORM. Of course, bypassing seems like a terrible idea. Is there a way to do this?
The Solution
I would like to bypass the entity validation for a specific column, as I'm using a custom extension, pgvector, in my database. Defining the type in the TypeORM entity results in a validation error, since the vector datatype isn't natively supported by PostgreSQL.
As a side note, I've installed the extension manually using the command CREATE EXTENSION vector.
Considered Alternatives
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don't know how to start. I would need guidance.
I no longer have the time as I'm not actively working on projects that use TypeORM.
Reacted by Vyacheslav Frolov, Lucien Loiseau, Dmytro Kharchenko, Greg Foster, Hagen Hübel , Grant Magdanz, dagyu, Sam Stenner, Benjamin Poirier, inde153 and 12 more
Reacted by Hagen Hübel , inde153 and Moritz Wallawitsch
changed the title [-]Modify `driver.supportedDataTypes` to support installed Postgres extension data type. Can I bypass validation for a column?[/-][+]Modify `driver.supportedDataTypes` to support installed Postgres extension data type. Bypass validation for a column.[/+]on May 17, 2023
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string.
See langchain-ai/langchainjs#1436 for reference.
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string. See hwchase17/langchainjs#1436 for reference.
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string. See hwchase17/langchainjs#1436 for reference.
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string. See hwchase17/langchainjs#1436 for reference.
Not when it comes to generating migrations
Migrations are in fact raw SQL!
That is a fact, I don't know what else they might be. This ORM happens to generate that SQL based on the column annotation. So no, as far as TypeORM is concerned this is not just a string.
I could create this column using raw SQL, but that defeats the purpose of using an ORM.
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string. See hwchase17/langchainjs#1436 for reference.
Not when it comes to generating migrations
Migrations are in fact raw SQL!
That is a fact, I don't know what else they might be. This ORM happens to generate that SQL based on the column annotation. So no, as far as TypeORM is concerned this is not just a string.
I happen to write my own migrations (old habits). However, I don't think modifying the generated SQL and replacing character varying with vector would be a problem. Everything else works just the same.
I worked with other ORMs, and all of them that I remember have some form to define custom data types, this happens in a lot of databases, PostgreSQL is one of this cases where can be user defined types, extensions, and so on.
Supporting custom column data types allow for all this use cases to fit the common use case for the library, we are exploring this kind of modification. At this time for example we have some tables that have a xid8 data type, that is not supported by typeorm. We workaround by marking the entity as not synced and making all the migration changes by hand, not very fun.
Reacted by Firtina Ozbalikchi, Roger Padilla and Rabin Bhandari
constAppDataSource=newDataSource({// your config})// @ts-ignore TypeORM does not support but the database supportsAppDataSource.driver.supportedDataTypes.push('vector')// add your datatypes hereAppDataSource.initialize()
To work with Nest.js TypeOrmModule:
TypeOrmModule.forRootAsync({// ... your configdataSourceFactory: async(options)=>{constdataSource=newDataSource(options)// @ts-ignore TypeORM does not support but the database supportsdataSource.driver.supportedDataTypes.push('vector')awaitdataSource.initialize()returndataSource}}),
Not sure if this will break any functions, but at least it creates table.
Hope this helps.
Reacted by lizhiyu and Saren
Reacted by Rohit Khatri
Reacted by diegobustosk, Vitaly Sivkov, Aleksandr Beshkenadze, lizhiyu, JCorona966 and mciriacocsd
Even after adding this, typescript still gives error when using
@Column({type: 'vector'})
embedding: any;
Below is the error:
No overload matches this call.
The last overload gave the following error.
Object literal may only specify known properties, and 'type' does not exist in type '(type?: any) => Function'.
Even after adding this, typescript still gives error when using
@Column({type: 'vector'})
embedding: any;
Below is the error:
No overload matches this call.
The last overload gave the following error.
Object literal may only specify known properties, and 'type' does not exist in type '(type?: any) => Function'.
This issue arises from TypeScript’s strict type checking. Since TypeORM does not natively support the vector type, its type definitions do not recognize it. To bypass this, you can use @ts-expect-error:
This suppresses the TypeScript error while still allowing the column definition to work.
Keep in mind that the dataSourceFactory approach mentioned earlier is intended to bypass TypeORM’s runtime type checks. However, TypeScript’s type system operates at compile time, so you’ll need both workarounds to ensure smooth functionality.
@Entity()exportclassLangchainFeedextendsAbstractUuidEntity<LangchainFeed>{
@Column({type: 'vector'asany,nullable: true})// Use type vector here but need to cast as any, otherwise TS will show an errorembedding_vector: number[];
@Column({type: 'text'})document_content: string;
@Column({type: 'text'})collection_name: string;
@Column({type: 'jsonb',nullable: true})extra_metadata: Record<string,any>;
@Column({type: 'int',nullable: true})chunk_size: number;
@Column({type: 'int',nullable: true})chunk_overlap: number;
@Column({type: 'text',nullable: true})group: string;
@BeforeUpdate()
@BeforeInsert()stringifyVector(){// Convert number array to string format, so that Postgres understands it as a vector// e.g. '[0.9, 0.1, 0.7]'if(this.embedding_vector&&Array.isArray(this.embedding_vector)){this.embedding_vector=JSON.stringify(this.embedding_vector)asany;}}}
Wanted to drop my workaround here in case it saves someone a debugging session — I actually shipped a migration that dropped the column before I noticed.
If you leave the vector column unmapped on the entity (because @column({ type: "vector" }) throws DataTypeNotSupportedError), the migration generator decides the column is "extra" and emits DROP COLUMN "embedding" next time someone runs migration:generate. Same thing for the HNSW index — TypeORM doesn't know about USING hnsw so it just wants to nuke it.
The catch here is that supportedDataTypes is set in the driver's constructor, not on the prototype — so trying to push to PostgresDriver.prototype.supportedDataTypes at import time blows up with "Cannot read properties of undefined". I hooked connect() instead, which runs once per DataSource before any entity validation:
// pgvector-typeorm.tsimport{fromSql,toSql}from"pgvector";import{ValueTransformer}from"typeorm";import{PostgresDriver}from"typeorm/driver/postgres/PostgresDriver";constVECTOR_TYPE="vector"asnever;constoriginalConnect=PostgresDriver.prototype.connect;PostgresDriver.prototype.connect=function(this: PostgresDriver){if(this.supportedDataTypes&&!this.supportedDataTypes.includes(VECTOR_TYPE)){this.supportedDataTypes.push(VECTOR_TYPE);}returnoriginalConnect.call(this);};exportconstvectorTransformer: ValueTransformer={to: (v: number[]|null|undefined)=>(v==null ? v : toSql(v)),from: (v: string|null)=>(v==null ? v : fromSql(v))};
Import this to your ormconfig.ts (or wherever your DataSource lives), so it runs both for the app and for the CLI:
Activity
[-]Modify `driver.supportedDataTypes` to support installed Postgres extension data type. Can I bypass validation for a column?[/-][+]Modify `driver.supportedDataTypes` to support installed Postgres extension data type. Bypass validation for a column.[/+]Ginden commentedon May 17, 2023
You can use my work from this PR: #8625
I'm too busy to complete it, so feel free to rebase/merge and add some tests.
purerosefallen commentedon Jun 14, 2023
How about directly support
pgvectorin TypeORM? as currently AIGC becoming a thing.dasheck0 commentedon Jun 19, 2023
+1
Thore1954 commentedon Jun 25, 2023
ORMs are simply a mapping between tables and classes. As far as typeorm is concerned, the vector datatype is just a string.
See langchain-ai/langchainjs#1436 for reference.
Marlinski commentedon Jul 8, 2023
+1 much needed!
wpride commentedon Jul 28, 2023
Not when it comes to generating migrations
Thore1954 commentedon Jul 29, 2023
Migrations are in fact raw SQL!
wpride commentedon Jul 29, 2023
That is a fact, I don't know what else they might be. This ORM happens to generate that SQL based on the column annotation. So no, as far as TypeORM is concerned this is not just a string.
Thore1954 commentedon Jul 29, 2023
I happen to write my own migrations (old habits). However, I don't think modifying the generated SQL and replacing
character varyingwithvectorwould be a problem. Everything else works just the same.Thore1954 commentedon Jul 29, 2023
That being said, I do believe it's a good idea to support pgvector out of the box similar to PostGIS (#2423).
nrathi commentedon Sep 3, 2023
What's the right workaround type for a vector column:
@Column({ type: "jsonb" })or@Column("numeric", { array: true })?mlois-efimob commentedon Mar 11, 2024
I worked with other ORMs, and all of them that I remember have some form to define custom data types, this happens in a lot of databases, PostgreSQL is one of this cases where can be user defined types, extensions, and so on.
Supporting custom column data types allow for all this use cases to fit the common use case for the library, we are exploring this kind of modification. At this time for example we have some tables that have a xid8 data type, that is not supported by typeorm. We workaround by marking the entity as not synced and making all the migration changes by hand, not very fun.
rabin999 commentedon Jul 2, 2024
It's very painful to achieve this due to the lack of extensibility, any lead on this ?
bkmashiro commentedon Aug 31, 2024
This may bypass datatype check:
To work with Nest.js TypeOrmModule:
Not sure if this will break any functions, but at least it creates table.
Hope this helps.
itsisaf commentedon Feb 4, 2025
This worked for me,
As bkmashiro said Not sure if this will break any functions, but at least it creates table.
rohitkhatri commentedon Mar 1, 2025
Even after adding this, typescript still gives error when using
Below is the error:
bkmashiro commentedon Mar 2, 2025
This issue arises from TypeScript’s strict type checking. Since TypeORM does not natively support the
vectortype, its type definitions do not recognize it. To bypass this, you can use@ts-expect-error:This suppresses the TypeScript error while still allowing the column definition to work.
Keep in mind that the
dataSourceFactoryapproach mentioned earlier is intended to bypass TypeORM’s runtime type checks. However, TypeScript’s type system operates at compile time, so you’ll need both workarounds to ensure smooth functionality.draungminoo commentedon Mar 6, 2025
app.module.tsentity.tsabstract-uuid-entity.tsnilsabegg commentedon Aug 7, 2025
@draungminoo Thank you so much, worked for me!
piotr-szewczyk commentedon May 23, 2026
Wanted to drop my workaround here in case it saves someone a debugging session — I actually shipped a migration that dropped the column before I noticed.
If you leave the vector column unmapped on the entity (because @column({ type: "vector" }) throws DataTypeNotSupportedError), the migration generator decides the column is "extra" and emits DROP COLUMN "embedding" next time someone runs migration:generate. Same thing for the HNSW index — TypeORM doesn't know about USING hnsw so it just wants to nuke it.
Import this to your ormconfig.ts (or wherever your DataSource lives), so it runs both for the app and for the CLI: