Search code examples
servicestackormlite-servicestack

OrmLite Model not populating after upgrading ServiceStack.Net from 4.0.39 to 4.5.6


After upgrading ServiceStack.Net from 4.0.39 to 4.5.6 we're seeing some cases where the model isn't being populated, however it does contain the correct amount of records. The properties of each record are all default values. Everything works when reverted back to 4.0.39.

We're wondering if something has changed that needs to be updated? We didn't notice anything in the docs and usage looks similar to examples.

If an answer isn't known, what would be the simplest next possible way to find out what is going on? (Looks like may need to download source and compile and reference new 4.5.6 OrmLite, but is there an easier option?)

  • It works in other very similar cases (# of params and table names are all that are different). We don't notice any difference between cases. The cases that work and don't work are executed in sequence. Changing the sequence doesn't change results.
  • Executing the sql in a db client (psql) returns values, so there is values to be populated.
  • No errors are thrown.
  • Postgres Dialect is being used.

Here is a simple example. https://gist.github.com/anonymous/bb514062a7d97b0ff4b0a546ef315765

using System;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.Configuration;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;

namespace MyCompany.Tests
{
    public class OrmLiteModelArrayTests
    {
        [Alias("color")]
        public class ColorModel
        {
            public string Color { get; set; }
            public string Value { get; set; }
        }

        public class ColorJsonModel
        {
            public int Id { get; set; }
            public string ColorJson { get; set; }
        }

        [Test]
        public void test_model_with_array_to_json()
        {

            OrmLiteConfig.DialectProvider = PostgreSqlDialect.Provider;

            var testingConn = ConfigUtils.GetConnectionString("testing");

            using (var db = testingConn.OpenDbConnection())
            {
                db.DropAndCreateTable();

                db.Insert(new ColorModel { Color = "red", Value = "#f00" });
                db.Insert(new ColorModel { Color = "green", Value = "#0f0" });
                db.Insert(new ColorModel { Color = "blue", Value = "#00f" });
                db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" });
                db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" });
                db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" });
                db.Insert(new ColorModel { Color = "black", Value = "#000" });

                const string sql = @"SELECT 1::integer AS id
                                        , json_agg(color.*) AS color_json
                                    FROM color;";

                var results = db.Select<ColorJsonModel>(sql);

                Assert.That(results.Count, Is.EqualTo(1));

                foreach (var result in results)
                {
                    Console.WriteLine("{0}".Fmt(result.ColorJson));
                    Assert.That(result.Id, Is.EqualTo(1));
                    Assert.That(result.ColorJson, Is.Not.Null);
                }

            }
        }

        [Test]
        public void test_model_with_array_and_json()
        {

            OrmLiteConfig.DialectProvider = PostgreSqlDialect.Provider;

            var testingConn = ConfigUtils.GetConnectionString("testing");

            using (var db = testingConn.OpenDbConnection())
            {
                db.DropAndCreateTable<ColorModel>();

                db.Insert(new ColorModel { Color = "red", Value = "#f00" });
                db.Insert(new ColorModel { Color = "green", Value = "#0f0" });
                db.Insert(new ColorModel { Color = "blue", Value = "#00f" });
                db.Insert(new ColorModel { Color = "cyan", Value = "#0ff" });
                db.Insert(new ColorModel { Color = "magenta", Value = "#f0f" });
                db.Insert(new ColorModel { Color = "yellow", Value = "#ff0" });
                db.Insert(new ColorModel { Color = "black", Value = "#000" });

                // SQL contains array and json aggs.
                // We usually have ARRAY fields defined in the db, but when
                // retrieved we json-ize them. In otherwords the array exists in the tables/views.
                // We use SELECT.* which would contain the ARRAY field.
                // Array fields are not used in any of our models and should not cause the other
                // fields in the model to not be populated.
                const string sql = @"SELECT 1::integer AS id
                                            , json_agg(color.*) AS color_json
                                            , array_agg(color.*) AS color_array
                                    FROM color;";

                var results = db.Select<ColorJsonModel>(sql);

                Assert.That(results.Count, Is.EqualTo(1));

                foreach (var result in results)
                {
                    Console.WriteLine("{0}".Fmt(result.ColorJson));
                    Assert.That(result.Id, Is.EqualTo(1));
                    Assert.That(result.ColorJson, Is.Not.Null);
                }

            }
        }
    }
}

Update:

What's weirder is an instance where we found an example where 7 total records are returned, but 6 of the 7 have default null values and the last (7th) record is populated fully.

Update 2:

The issue is dealing with ARRAY fields that are contained in the SELECT * but are not used in the C# models.


Solution

  • The sample code in the description doesn't compile but I've tested the closest working version possible with the latest version of OrmLite and progress which is working as expected:

    [Alias("my_table")]
    public class MyModel
    {
        public int MyId { get; set; }
        public int BlahId { get; set; }
        public string Name { get; set; }
    }
    
    using (var db = OpenDbConnection())
    {
        db.DropAndCreateTable<MyModel>();
    
        db.Insert(new MyModel { MyId = 1, BlahId = 2, Name = "foo" });
        db.Insert(new MyModel { MyId = 2, BlahId = 3, Name = "bar" });
    
        var results = db.GetMyModels(1, 2);
    
        Assert.That(results.Count, Is.EqualTo(1));
        Assert.That(results[0].MyId, Is.EqualTo(1));
        Assert.That(results[0].BlahId, Is.EqualTo(2));
        Assert.That(results[0].Name, Is.EqualTo("foo"));
    }
    
    public static class CustomSqlExtensions
    {
        public static List<MyModel> GetMyModels(this IDbConnection db, int myId, int blahId)
        {
            const string sql = @"SELECT * FROM my_table WHERE my_id = @myId AND blah_id = @blahId";
            return db.Select<MyModel>(sql, new { myId, blahId });
        }
    }
    

    Whilst it wont have an impact here, when using Custom SQL you should use the Custom SQL APIs like SqlList, e.g:

    return db.SqlList<MyModel>(sql, new { myId, blahId });
    

    The issue highlighted from this Gist:

    const string sql = @"SELECT 1::integer AS id
                                , json_agg(color.*) AS color_json
                                , array_agg(color.*) AS color_array
                        FROM color;";
    
    var results = db.Select<ColorJsonModel>(sql);
    

    Using unknown PostgreSQL Types in Npgsql

    Is due to Npgsql not knowing what the array_agg Type is so will throw the Exception:

    The field 'color_array' has a type currently unknown to Npgsql (OID 101487)

    When trying to read the row dataset with its GetValues() batch API. I've added a fallback in this commit where if the GetValues() batch API fails it will fallback to individual fetch reads which will resolve this issue.

    However this throws an Exception which isn't optimal, you can instead tell OrmLite to avoid using the batch GetValues() API altogether by telling it to always use individual field fetches with:

    OrmLiteConfig.DeoptimizeReader = true;
    

    Which will avoid attempting to use GetValues() and avoid its Exception.