Skip to content

Inconsistent casing rules for type mapping within composite types #855

Closed

Description

@lukas-shawford

Type mapping appears to be case-insensitive for types that are directly on the row (top-level), but case sensitive if the type appears within a composite type.

Example: I have a composite type that contains an enum:

CREATE TYPE DistanceUom AS ENUM
(
    'Feet',
    'Meters'
);


CREATE TYPE Distance AS
(
    Value numeric,
    Unit DistanceUom
);

In C#, I have an enum for DistanceUom and a CLR type for Distance :

public enum DistanceUom { Feet, Meters }

public class Distance
{
    public decimal Value { get; set; }

    public DistanceUom Unit { get; set; }
}

I registered both the enum and the composite type:

NpgsqlConnection.MapEnumGlobally<DistanceUom>();
NpgsqlConnection.MapCompositeGlobally<Distance>();

However, when I attempt to query a table that has a Distance column, I get the error:

PostgreSQL composite type distance contains field unit which could not match any on CLR type Distance

If I rename the Unit property in my Distance class to lowercase unit, then it works fine (though it breaks C# capitalization conventions).

If I query for the DistanceUom enum directly, the casing doesn't seem to matter - it's able to map the PostgreSQL enum to the C# enum just fine, regardless of case:

// works
con.Query<DistanceUom>("SELECT (Distance).Unit FROM Distances;");

// also works
con.Query<DistanceUom>("SELECT (Distance).Unit AS \"uNIT\" FROM Distances;");

So it appears that type handling is case-insensitive if the DistanceUom enum appears directly on the returned row, but case sensitive if it appears within a composite type. Ideally these ought to be consistent.

Activity

  1. self-assigned this
    on Nov 7, 2015
  2. roji commented on Nov 8, 2015

    @roji
    Member

    @SergKr, I finally had time to look at this and the issue seems to be much simpler... In PostgreSQL, identifiers which have uppercase letters must be quoted, otherwise PostgreSQL converts them to lowercases; the CREATE TYPE statements in your example must do this. In other words, when you specified an unquoted Unit, PostgreSQL itself converted it to lowercase unit. You can confirm this by doing \d distance in psql (note that it's lowercase distance, because that was lowercased as well).

    Can you please try with the following statements:

    CREATE TYPE "DistanceUom" AS ENUM
    (
        'Feet',
        'Meters'
    );
    
    
    CREATE TYPE "Distance" AS
    (
        "Value" numeric,
        "Unit" "DistanceUom"
    );
  3. lukas-shawford commented on Nov 10, 2015

    @lukas-shawford
    Author

    Interesting, that results in a different error for me. I have to expand my example a bit, since the error occurs one level higher.

    Here's the complete PostgreSQL code (with all names quoted):

    CREATE TYPE "DistanceUom" AS ENUM
    (
        'Feet',
        'Meters'
    );
    
    
    CREATE TYPE "Distance" AS
    (
        "Value" numeric,
        "Unit" "DistanceUom"
    );
    
    CREATE TABLE Runways
    (
        Designator text,
        Length "Distance"
    );
    
    INSERT INTO Runways (Designator, Length)
    VALUES
    ('08L', ROW(5280, 'Feet')),
    ('36R', ROW(1244, 'Meters'));
    

    And here are the C# types:

    public enum DistanceUom { Feet, Meters }
    
    public class Distance
    {
        public decimal Value { get; set; }
    
        public DistanceUom Unit { get; set; }
    }
    
    public class Runway
    {
        public string Designator { get; set; }
    
        public Distance Length { get; set; }
    }
    

    Here's my query (also uses Dapper, though exception occurs in Npgsql):

    NpgsqlConnection.MapEnumGlobally<DistanceUom>();
    NpgsqlConnection.MapCompositeGlobally<Distance>();
    
    var connectionString = ConfigurationManager.ConnectionStrings["Demo"].ConnectionString;
    using (NpgsqlConnection con = new NpgsqlConnection(connectionString))
    {
        con.Open();
        var results = con.Query<Runway>("SELECT * FROM Runways");
    }
    

    The exception I'm getting is:

    The field length has a type currently unknown to Npgsql (OID 24883). You can retrieve it as a string by marking it as unknown, please see the FAQ.
    

    (It's referring to the Length property inside the Runway class).

    So when the Distance type is defined with a quoted name, Npgsql is no longer able to map it.

    I wonder if the quotes themselves become part of the type name if you define it that way? Just basing this off the way it's showing in pgAdmin:

    npgsql_distance

    In any case, I see you opened up #859 (pluggable field mapping convention), which I think would solve the original issue - you can feel free to close this one.

  4. roji commented on Nov 10, 2015

    @roji
    Member

    @SergKr, when you call MapCompositeGlobally or MapEnumGlobally without providing a PostgreSQL type name as a parameter (as in your example), Npgsql currently defaults to simply lowercasing the CLR type name and using that. So Npgsql is looking for a distance composite rather than a Distance one. Try passing the PostgreSQL type names explicitly when you're calling the mapping functions. Let me know if that helps.

    I agree this is all a bit messy at the moment, #859 is definitely supposed to make things better. Thanks for your comments and feedback!

    PS The quotes aren't part of the type name, I suspect it's just a display feature of PgAdmin or something.

  5. dlogik commented on Feb 2, 2016

    @dlogik

    A slightly different issue,
    I noticed if you try and query for the entire composite type e.g.:
    con.Query("SELECT Distance FROM Distances;");

    You also receive the message "PostgreSQL composite type distance contains field unit which could not match any on CLR type Distance"

    Are there plans to add support for populating nested enum types within a composite type in this manner?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions