pgvector-dotnet icon indicating copy to clipboard operation
pgvector-dotnet copied to clipboard

Writing values of 'Pgvector.Vector' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type

Open Issung opened this issue 9 months ago • 9 comments

Sorry I never do this but after 2 days I can't crack this nut and have reached my limit 😂

I've been getting this error when trying to save a type with a Vector column in it...

Writing values of 'Pgvector.Vector' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type..

   at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForWriting>g__ThrowWritingNotSupported|1_0(Type type, PgSerializerOptions options, Nullable`1 pgTypeId, Nullable`1 npgsqlDbType, Exception inner)
   at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForWriting(Type type, Nullable`1 pgTypeId, PgSerializerOptions options, Nullable`1 npgsqlDbType)
   at Npgsql.NpgsqlParameter.ResolveTypeInfo(PgSerializerOptions options)
   at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType)
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__120.MoveNext()
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__120.MoveNext()
   at Npgsql.NpgsqlCommand.<ExecuteDbDataReaderAsync>d__113.MoveNext()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__18.MoveNext()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__18.MoveNext()
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__50.MoveNext()

I've tried everything I can, today I created minimal projects following your guides, then another using IdentityDbContext thinking that may be the issue, nothing seems to fix it. I downloaded the Npgsql source code and mounted it to see where this exception was thrown and debug from there but couldn't figure it out much further.

My repo is relatively small it's just some prototyping with Razor Pages and your stuff. If you clone the commit linked below, startup the pg17 docker container, and try to run the DndTest project, it will try to do an insert of the type and reproduce the error immediately. The other projects are attempts at minimal reproductions but they don't reproduce 😭

Or if you just take a look at the source code or have some recommendations of things to try before doing that it would all be greatly appreciated 🙏

https://github.com/Issung/SemanticDnd/commit/83d430bfc82567575f6262a1f06abbedc23d1811

Issung avatar Apr 02 '25 10:04 Issung

Hi @Issung, this looks like the same error as #31 and #41 (likely missing a call to useVector somewhere).

ankane avatar Apr 03 '25 18:04 ankane

Thanks for the reply @ankane. I have this in my Program.cs. It works well enough that it is able to create the initial migration. I'll try doing it in the DbContext's OnConfiguring override and see if that fixes it.

builder.Services.AddDbContext<DndDbContext>(options =>
{
    options.UseNpgsql(connectionString, o => o.UseVector());
    options.EnableSensitiveDataLogging();
});

Issung avatar Apr 03 '25 21:04 Issung

That didn't fix it..

I cloned my project and started trimming things out until the program started to work.. What makes it or breaks it is app.UseHangfireDashboard()... I guess because it also uses the postgres database? Very bizarre. I can try to make a minimal reproducible example, but not sure who the fix would need to come from, you guys or the Hangfire.PostgreSql guys.. 😅

Issung avatar Apr 05 '25 06:04 Issung

Looks like others have run into this as well: https://github.com/hangfire-postgres/Hangfire.PostgreSql/issues/316

It sounds like Hangfire.PostgreSql has the same issue with NodaTime and there's a workaround: https://github.com/hangfire-postgres/Hangfire.PostgreSql/issues/322#issuecomment-1710694316

ankane avatar Apr 05 '25 06:04 ankane

We may be able to remove the NpgsqlConnection.GlobalTypeMapper call like other extensions did in https://github.com/npgsql/efcore.pg/pull/3167, which may fix it.

ankane avatar Apr 05 '25 08:04 ankane

Switched to the new plugin approach in this branch, but it requires EF Core 9.

ankane avatar Apr 05 '25 09:04 ankane

Mre.zip

You're a couple steps ahead of me! Here's the minimal reproducible example. If it's not of use to you any more maybe it will be for someone else in the future, so I'll upload it anyway :)

Issung avatar Apr 05 '25 10:04 Issung

Switched to the new plugin approach in this branch, but it requires EF Core 9.

@ankane looks great, this is definitely the right direction from 9.0 onwards.

roji avatar Apr 05 '25 13:04 roji

@roji Thanks for taking a look.

@Issung I probably won't merge / drop support for EF Core 8 until EF Core 10 is released, so I'd recommend using the workaround linked above in the meantime.

ankane avatar Apr 05 '25 20:04 ankane

Hi @ankane , I have the same error. I use EF core 8 and not missing UseVector(). Can you give me how to fix that error.

---> Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> System.InvalidCastException: Writing values of 'Pgvector.Vector' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type.. at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForWriting>g__ThrowWritingNotSupported|1_0(Type type, PgSerializerOptions options, Nullable1 pgTypeId, Nullable1 npgsqlDbType, Exception inner) at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForWriting(Type type, Nullable1 pgTypeId, PgSerializerOptions options, Nullable1 npgsqlDbType) at Npgsql.NpgsqlParameter.ResolveTypeInfo(PgSerializerOptions options) at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType) at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

tuyetlangsa avatar Jun 30 '25 14:06 tuyetlangsa

Same problem here

[04:41:43 ERR] An exception occurred while iterating over the results of a query for context type 'Equibles.Core.Data.Contexts.ApplicationDbContext'.
System.InvalidCastException: Writing values of 'Pgvector.Vector' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type..                                                                                                                                                                
   at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForWriting>g__ThrowWritingNotSupported|1_0(Type type, PgSerializerOptions options, Nullable`1 pgTypeId, Nullable`1 npgsqlDbType, Exception inner)                                                                                                                                                                
   at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForWriting(Type type, Nullable`1 pgTypeId, PgSerializerOptions options, Nullable`1 npgsqlDbType)                              
   at Npgsql.NpgsqlParameter.ResolveTypeInfo(PgSerializerOptions options)                                                                                                           
   at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType)                                              
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)                                                              
   at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)                                                              
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)                                                                  
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)             
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)             
   at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken) 
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)                                                                                                                                                          
   at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()      

I am calling UseVector()

daniel3303 avatar Aug 24 '25 03:08 daniel3303

@ankane, any expectation on when to merge and release the config-plugin branch? As the library is not usable in its current version. EF Core 8 users can opt not to upgrade to the most recent version.

daniel3303 avatar Aug 26 '25 14:08 daniel3303

Hi! Workaround, just added to my config in Program.cs:

#pragma warning disable CS0618
//Vector type workaround
NpgsqlConnection.GlobalTypeMapper.UseVector();
#pragma warning restore CS0618

During play with this issue I also updated to Net 9.0 with Core 9.0 and so on, don't want to revert so cannot say if it helps for 8.0

rsmolnikov avatar Sep 03 '25 23:09 rsmolnikov

Try to integrate pgvector with NHibernate, got the same error, I'm sure I have called UseVector() method, because I can query Vector but can not save it.

Full code is https://github.com/beginor/nhibernate-extensions/tree/master/src/NHibernate.Extensions.Pgvector.

temp save workaround is:

https://github.com/beginor/nhibernate-extensions/blob/58f6c1b20e39d8e305ff5e3e09c43351101de7e6/src/NHibernate.Extensions.Pgvector/UserTypes/VectorType.cs#L38

but can used with any distance operators like <=>

beginor avatar Nov 03 '25 07:11 beginor

Merged the new plugin approach in the commit above.

ankane avatar Nov 11 '25 20:11 ankane