PetaPoco
PetaPoco copied to clipboard
When join and multi mapping, split problem because column name lower/upper case mixed, and howto fix.
I have two table like below. (poco class)
[PetaPoco.TableName("conversion_request")]
[PetaPoco.PrimaryKey("idxno", AutoIncrement = true)]
public class ConvRequest
{
[PetaPoco.Column("idxno")] // <--- lower case column name
public Int64 Idxno { get; set; }
[PetaPoco.Column("CONTENT_ID")]
public string ContentId { get; set; }
[PetaPoco.Column("priority")]
public Int64 Priority { get; set; }
[PetaPoco.Ignore]
public Content Content { get; set; }
}
[PetaPoco.TableName("content")]
[PetaPoco.PrimaryKey("IDXNO", AutoIncrement = false)]
public class Content
{
[PetaPoco.Column("IDXNO")] // <--- upper case column name
public Int64 Idxno { get; set; }
[PetaPoco.Column("CONTENT_ID")]
public string ContentId { get; set; }
[PetaPoco.Column("USERID")]
public string UserId { get; set; }
}
query like below
string sql = @"select conversion_request.*, content.*
from conversion_request
join content
on conversion_request.CONTENT_ID = content.CONTENT_ID";
var result = db.Query<ConvRequest, Content, ConvRequest>(
(req, con) => { req.Content = con; return req; }, sql)
.ToList();
MySQL output like below
+-------+---------------+----------+-------+---------------+---------+
| idxno | CONTENT_ID | priority | IDXNO | CONTENT_ID | USERID |
+-------+---------------+----------+-------+---------------+---------+
| 1 | 56b2da13ab7b6 | 99 | 1167 | 56b2da13ab7b6 | uid0004 |
+-------+---------------+----------+-------+---------------+---------+
but the result was result[0].Idxno == 1167
and result[0].Content.Idxno == 0
And I fixed problem by editing MultiPocoFactory.FindSplitPoint()
// Find split point
var firstColumn = pos;
//var usedColumns = new Dictionary<string, bool>(); // <--- original (nuget.org Petapoco 5.1.140)
var usedColumns = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase); // <--- my fix
for (; pos < r.FieldCount; pos++)
@wafe thanks for this. I'll address this in the next release
I've run into this issue as well. I'm joining a few tables with similar ID names ("Id", "ID"). The results are being converted into objects using Petapoco. While the SQL query returns the correct column results, Petapoco is mapping the ID columns incorrectly.
Example:
Id Id ID Id
35 6 NULL 21
Here are the ID results for a query with multiple joins. (Other columns have been removed for example purposes) Petapoco is attempting to map the results to objects. However, the value for ID (NULL) is instead being set using the value of the following Id (21). This is incorrect.
Result from Petapoco:
Id Id ID Id
35 6 21 0