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
Inconsistent casing rules for type mapping within composite types #855
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:
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.
@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:
CREATETYPE "DistanceUom" AS ENUM
(
'Feet',
'Meters'
);
CREATETYPE "Distance" AS
(
"Value"numeric,
"Unit""DistanceUom"
);
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:
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.
@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.
Activity
roji commentedon Nov 8, 2015
@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 TYPEstatements in your example must do this. In other words, when you specified an unquotedUnit, PostgreSQL itself converted it to lowercaseunit. You can confirm this by doing\d distancein psql (note that it's lowercasedistance, because that was lowercased as well).Can you please try with the following statements:
lukas-shawford commentedon Nov 10, 2015
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):
And here are the C# types:
Here's my query (also uses Dapper, though exception occurs in Npgsql):
The exception I'm getting is:
(It's referring to the
Lengthproperty inside theRunwayclass).So when the
Distancetype 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:
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.
roji commentedon Nov 10, 2015
@SergKr, when you call
MapCompositeGloballyorMapEnumGloballywithout 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 adistancecomposite rather than aDistanceone. 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.
dlogik commentedon Feb 2, 2016
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?