Skip to content

Npgsql.NpgsqlCommand.DeriveParametersForQuery fails on un-mapped Enums #1736

Closed

Description

@dmitry-a-morozov

Exception message:

'No mapping found for PostgreSQL type [MyEnum]'

Stack trace:

   at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
   at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
   at Npgsql.NpgsqlCommand.DeriveParameters()
   at Npgsql.NpgsqlCommandBuilder.DeriveParameters(NpgsqlCommand command)
   ...

Further technical details

Npgsql version: 3.3.0-command-caching
PostgreSQL version: 9.6.5
Operating system: Win10

Other details:
I think it fails for mapped enums too with

System.InvalidOperationException: Invalid parameter type: 55253
   at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
   at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
   at Npgsql.NpgsqlCommand.DeriveParameters(

Activity

  1. added this to the milestone on Dec 2, 2017
  2. roji commented on Dec 2, 2017

    @roji
    Member

    @Brar, want to take a look?

    @dmitry-a-morozov FYI it's not a good idea to work with 3.3.0-command-caching (although I know it's the one that appears on the top of the nuget list) - work with the latest 3.3.0-ci instead.

  3. dmitry-a-morozov commented on Dec 2, 2017

    @dmitry-a-morozov
    ContributorAuthor

    I was using 3.3.0-ci but switched to 3.3.0-command-caching hoping that it has the problem fixed

  4. Brar commented on Dec 2, 2017

    @Brar
    Member
  5. dmitry-a-morozov commented on Dec 4, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @Brar It also fails for array types like interger[] too with

    System.InvalidOperationException
      HResult=0x80131509
      Message=No mapping found for PostgreSQL type _int4
      Source=Npgsql
      StackTrace:
       at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
       at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
       at Npgsql.NpgsqlCommand.DeriveParameters()
       at Npgsql.NpgsqlCommandBuilder.DeriveParameters(NpgsqlCommand command)
    
  6. Brar commented on Dec 4, 2017

    @Brar
    Member

    Thanks @dmitry-a-morozov for reporting this. I'll look at all the non-commonplace-types while at it.

  7. Brar commented on Dec 4, 2017

    @Brar
    Member

    @dmitry-a-morozov please test #1740 if it solves the problems for you.

  8. dmitry-a-morozov commented on Dec 5, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @Brar It works for arrays. For example, a parameter of integer[] has correctly inferred to value NpgsqlDbType.Array | NpgsqlDbType.Integer of Npgsql​Parameter.NpgsqlDbType property.
    Not so much for enumerations. I hoped to get Npgsql​Parameter.NpgsqlDbType set to NpgsqlDbType.Enum and Npgsql​Parameter.SpecificType set to a name of custom enumeration but instead I get Npgsql​Parameter.NpgsqlDbType equals to NpgsqlDbType.Text and Npgsql​Parameter.SpecificType is null.

  9. Brar commented on Dec 5, 2017

    @Brar
    Member

    I hoped to get Npgsql​Parameter.NpgsqlDbType set to NpgsqlDbType.Enum and Npgsql​Parameter.SpecificType set to a name of custom enumeration but instead I get Npgsql​Parameter.NpgsqlDbType equals to NpgsqlDbType.Text and Npgsql​Parameter.SpecificType is null.

    I could easily fix Npgsql​Parameter.NpgsqlDbType, as it is a manual assignment but I can't see a way to infer Npgsql​Parameter.SpecificType for an unmapped enum. There simply isn't a way how we could know the type to infer from inside Npgsql code.
    @roji - is the behavior cited above (set Npgsql​Parameter.NpgsqlDbType to NpgsqlDbType.Enum also for unmapped Enums) ok/intended? Could we leave Npgsql​Parameter.SpecificType empty until an actual enum value is set to the parameter and infer it at this time?

  10. dmitry-a-morozov commented on Dec 5, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @Brar You're right Npgsql​Parameter.SpecificType property cannot be set because it requires actual .NET type. My bad, I thought it's of type string. Is there way to store enum name somewhere in Npgsql​Parameter instance as it appears in database?

  11. roji commented on Dec 5, 2017

    @roji
    Member

    Hmm... Npgsql does support reading/writing unmapped enums as text. For reading this is pretty straightforward, but for writing IIRC you need to leave NpgsqlDbType to Unknown (i.e. if you set it to NpgsqlDbType.Text you'll get a PostgreSQL cast error).

    This makes it a bit tricky... If the enum has been mapped, we should of course infer everything (NpgsqlDbType, SpecificType) because we can. But what to do about the unmapped case? IIRC trying to send a parameter with NpgsqlDbType set to Enum, but without also setting SpecificType results in a failure - you must either set both or set none - so it doesn't make much sense to me to do this. I think maybe it's just best to leave at NpgsqlDbType Unknown...

    If not, maybe it's better to leave NpgsqlDbType at Unknown

  12. Brar commented on Dec 5, 2017

    @Brar
    Member

    For reading this is pretty straightforward, but for writing IIRC you need to leave NpgsqlDbType to Unknown (i.e. if you set it to NpgsqlDbType.Text you'll get a PostgreSQL cast error).

    Well, it obviously depends on what value you are actually trying to write or, in other words, what your client side representation of the PostgreSQL enum is. If it is an actual .Net enum you probably can't send it as NpgsqlDbType.Text but chances are, that your client side representation is just simple string values as it is the case in the test DeriveParametersCommandTypeTextUnmappedEnum in #1740 which would certainly work. After all there probably is a reason why you didn't map the enum in the first place (i. e. targeting multiple RDBMS where most of them don't have a concept of mappable enums).
    Anyways, this are just my assumptions of probable use cases and I could certainly live with returning NpgsqlDbType.Unknown for unmapped enums.

    What do you think of the feature request from @dmitry-a-morozov about somehow storing the database type name in instances of Npgsql​Parameter.
    While it is certainly possible to add a Property to Npgsql​Parameter and set it when deriving parameters it will add complexity and possible corner cases, which is why I'm uncertain if the use case justifies the addition of this feature.
    Perhaps we should get to know more about the actual use case.

  13. dmitry-a-morozov commented on Dec 5, 2017

    @dmitry-a-morozov
    ContributorAuthor

    Primary use case:
    I'm building F# type provider for postgres similar to one I did for Sql Server
    http://fsprojects.github.io/FSharp.Data.SqlClient/
    It's like code generation on steriods.
    Based on a query command type generated at design time with Execute/AsyncExecute methods. During execution NpgsqlCommand is created, proper parameters set up and values passed into Execute mapped to parameter values.
    So, I'm going backward - parameters and enums created based on meta-information received from DeriveParameters.

    I do think that having string type property that contains user defined type is useful because an expectation that there real type exists is a little restrictive. It will mirror behavior of Npgsql​Data​Reader where this info available for output columns. Also that how SqlClient solves the issue:
    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparameter.udttypename?view=netframework-4.7.1&viewFallbackFrom=netstandard-2.0

    @Brar My understanding that mapped enums still fail. Is it correct?

  14. roji commented on Dec 5, 2017

    @roji
    Member

    Well, it obviously depends on what value you are actually trying to write or, in other words, what your client side representation of the PostgreSQL enum is. If it is an actual .Net enum you probably can't send it as NpgsqlDbType.Text but chances are, that your client side representation is just simple string values as it is the case in the test DeriveParametersCommandTypeTextUnmappedEnum in #1740 which would certainly work. After all there probably is a reason why you didn't map the enum in the first place (i. e. targeting multiple RDBMS where most of them don't have a concept of mappable enums).

    If I understand you correctly, then I pretty much agree. If there is an actual .NET enum that it should be mapped before derivation, in which case there's no problem. Otherwise, if a mapping hasn't been set up, I think it's safe to assume that interaction will be done via text, and the one thing I expect from the derivation process is to set up my NpgsqlParameter so that setting the value (i.e. to a string) would just work.

    What do you think of the feature request from @dmitry-a-morozov about somehow storing the database type name in instances of Npgsql​Parameter.

    While of course this is possible, it seems really odd to add a string property to NpgsqlParameter for the sole reason of... deriving parameter types for unmapped enums...! That seems like the most corner case scenario ever.

    Once again, I think we should always keep in mind what a program could do with the information provided by the deriving process. What could a program possibly do with a string type name, except maybe show it to the user? It seems quite useless to me.

  15. 16 remaining items

  16. Brar commented on Dec 10, 2017

    @Brar
    Member

    Ok, I've implemented NpgsqlParameter.PostgresType as discussed here (see #1740 ) except lazily loading of composite types, which will take some more time.

  17. dmitry-a-morozov commented on Dec 10, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @Brar Great job !
    I suggest create a separate PR for composite types so I won't be blocked on enums.

  18. Brar commented on Dec 10, 2017

    @Brar
    Member

    @dmitry-a-morozov thanks ;)
    Actually mapped composite types should already work perfectly. It's only unmapped composite types that currently don't get loaded by Npgsql.

    After reading carefully I've found another suggestion that isn't implemented yet:

    However, this makes me think that Npgsql could be providing you with more information about the types (e.g. enum values, composite fields and their types), to save you the trouble of going and getting that information from the database yourself. It's a matter of making the PostgresType type hierarchy expose richer information.

    While I made the composite type fields public in my PR I don't see any way to access enum values yet.
    This might also justify another pull request.

    @roji I agree with @dmitry-a-morozov about opening a separate PR for the remaining work and possibly even a new issue for discussion. After all, this one is marked as bug, which should be resolved by now and the more we discuss the more features creep into the PR which will probably increase the time you need to review it.

  19. roji commented on Dec 11, 2017

    @roji
    Member

    Great job @Brar! I'll try to review and merge asap. Ok for handling the unmapped composites in a separate PR.

  20. roji commented on Dec 11, 2017

    @roji
    Member

    @dmitry-a-morozov, no, there's no fallback for unmapped composites, and in fact having one isn't really possible. Enums are represented in the binary wire encoding as a simple string, which is why we can have the enum-as-string fallback - the user simply sends a string flagged as Unknown and postgresql performs an implicit cast.

    Composites, on the other hand, have a complex binary representation (number of fields, type code for each string, etc.). We would indeed have to add Npgsql support for handling unmapped composites, e.g. as dictionaries or even dynamic. That would be yet another PR.

  21. dmitry-a-morozov commented on Dec 11, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @roji That exactly what I thought - supporting dictionaries or dynamic !

  22. roji commented on Dec 11, 2017

    @roji
    Member

    OK, great. As suggested above, let's do this step by step. Providing a PostgresType for composites is one thing, exposing more info from PostgresType is another, and having Npgsql support unmapped composites is yet another...

  23. dmitry-a-morozov commented on Dec 11, 2017

    @dmitry-a-morozov
    ContributorAuthor

    Agree 100%. I don't have immediate need for composite types so take you take to think it through. No rush.

  24. Brar commented on Dec 11, 2017

    @Brar
    Member

    Ok, I've rebased the PR and squashed everything into one commit to make it easier to review.
    Have fun @roji ;)

  25. roji commented on Dec 12, 2017

    @roji
    Member

    Thanks @Brar, will try to review as soon as I can...

  26. roji commented on Dec 13, 2017

    @roji
    Member

    @Brar and @dmitry-a-morozov, check out #1746 for supporting unmapped composites as dynamic. I'm still not 100% what I want to do there but I'll probably end up implementing it. As it touches composite support in a more fundamental manner it's probably a good idea for me to tackle, although @Brar if you want a go at that issue as well let me know.

  27. Brar commented on Dec 14, 2017

    @Brar
    Member

    As it touches composite support in a more fundamental manner it's probably a good idea for me to tackle, although @Brar if you want a go at that issue as well let me know.

    Please go ahead. I'm currently working on nullable arrays which is hard enough for now.

  28. dmitry-a-morozov commented on Dec 15, 2017

    @dmitry-a-morozov
    ContributorAuthor

    @roji Contrary to your comment here #1736 (comment) I cannot find nuget package on myget unstable for example for build https://ci.appveyor.com/project/roji/npgsql/build/3.3.0-467. Do I look in wrong place?

  29. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions