Auto Query Bugs

Hi,
I use pre-release service stack 4.0.43.
Bugs: Can’t not get right items in the auto query
reproduce steps:

  1. i write my code like this

    [Authenticate]
    [RequiredRole(“Parent”)]
    [Route("/questions/parent/", “GET”)]
    public class QueryQuestionsByParent : QueryBase<UV_QuestionChild, QuestionChildResponse>
    {
    public long? QuestionIdGreaterThan { get; set; }

             public long? QuestionIdLessThan { get; set; }
     
             public long ChildId { get; set; }
         }
     [Alias("UV_QuestionChild")]
     	[Schema("dbo")]
         public partial class UV_QuestionChild 
         {
             [Required]
             public int QuestionId { get; set;}
             public string Content { get; set;}
             [Required]
             public int ChildId { get; set;}
             [Required]
             public DateTimeOffset CreateTime { get; set;}
             public int? QuestionType { get; set;}
             public int? SendUserId { get; set;}
             public DateTimeOffset? BeginDate { get; set;}
             public DateTimeOffset? EndDate { get; set;}
             [Required]
             public string ChildName { get; set;}
             public int? ParentId { get; set;}
             public string ParentDisplayName { get; set;}
             public string ChildImageName { get; set;}
             public int? SenderId { get; set;}
             public string SenderDisplayName { get; set;}
             public string SenderImage { get; set;}
         }
    

    public class QuestionChildResponse
    {
    public int QuestionId { get; set; }

         public string Content { get; set; }
    
         public int ChildId { get; set; }
    
         public DateTimeOffset CreateTime { get; set; }
    
         public int? QuestionType { get; set; }
    
         public int? SendUserId { get; set; }
    
         public DateTimeOffset? BeginDate { get; set; }
    
         public DateTimeOffset? EndDate { get; set; }
    
         public string ChildName { get; set; }
    
         public int? ParentId { get; set; }
    
         public string ParentDisplayName { get; set; }
    
         public string ChildImageName { get; set; }
    
         public int? SenderId { get; set; }
    
         public string SenderDisplayName { get; set; }
    
         public string SenderImage { get; set; }
     }
    

excepted: return right items
actually: return fields of items is null, but total is not null. if write code like this QueryBase<UV_QuestionChild, UV_QuestionChild > or QueryBase<UV_QuestionChild>
i can get results.


This is not an example of a stand-alone repro, i.e. this cannot be run by anyone else to reproduce the issue or be able to identify what it is. You don’t even include what the request was called with and UV_QuestionChild doesn’t have a primary key.

Have you checked to see if what the generated SQL is to see if it matches up with what you expect? Were there any warning or errors in the logs?

To create a repro, send a link to a stand-alone project on GitHub that can be run locally (i.e by anyone else) to reproduce the issue.

Thanks for replying,
I find when i connect to sqlserver, everything is OK.
The error occurs in my sqlite test DB.
I will check my DB later.
If you have time, please run SelfHostIntegrateTest and you will find all “Query Test” fail.
git@github.com:benhaben/SSTest.git

Sqlite doesn’t support schema’s, they’re just appended to the table name.
The [Schema("dbo")] is unnecessary for SQL Server which hurts interoperability with the other databases that doesn’t support schemas - I’ve removed it from the T4 templates as well.

Can you try after removing all [Schema] attributes for all models and check if that works. Please also see the reported issue in your repo.

Still can not work.

Could you update the code and debug Query_children_by_teacher_test
it throw exception now.

I already recreate sqlite db. the db is ok.

You’ve still not read and actioned the reported issue, please confirm when you’ve done so.

Sorry, I remove the repository now…
Did’t notice that before. I will not put license public. i hope it does not make any trouble.

If you enabled logging (as I’ve requested in my initial response)

LogManager.LogFactory = new ConsoleLogFactory(debugEnabled:true);

It will tell you what the exception was:

DEBUG: SQL: SELECT "UV_Child"."KindergartenId", "UV_Child"."KindergartenName", "UV_Child"."ClassId", "UV_Child"."ClassName", "UV_Child"."ChildId", "UV_Child"."ChildName", "UV_Child"."ParentId", "UV_Child"."DisplayName", "UV_Child"."UserName", "UV_Child"."PhoneNumber", "UV_Child"."ImageName" 
FROM "UV_Child"
WHERE "UV_Child"."ClassId" = 112
LIMIT 100
ERROR: ServiceBase<TRequest>::Service Exception, Exception: SQLite error
no such column: UV_Child.PhoneNumber

This is saying it can’t find PhoneNumber column. Which is strange because UV_Child does have a PhoneNumber column:

[Alias("UV_Child")]
public partial class UV_Child 
{
    public int? KindergartenId { get; set;}
    public string KindergartenName { get; set;}
    public int? ClassId { get; set;}
    public string ClassName { get; set;}
    [Required]
    public int ChildId { get; set;}
    [Required]
    public string ChildName { get; set;}
    public int? ParentId { get; set;}
    public string DisplayName { get; set;}
    public string UserName { get; set;}
    public string PhoneNumber { get; set;}
    public string ImageName { get; set;}
}

However I noticed you’re actually not using an in-memory Sqlite database, you’re instead using a pre-existing Sqlite database:

    var dbPath = "~/../dbSqlite.db".MapHostAbsolutePath();
    container.Register<IDbConnectionFactory>(
        c => new OrmLiteConnectionFactory(dbPath, SqliteDialect.Provider));

Which means I can’t verify that the schema in this database actually matches the model you’re trying to query, which also shows this error when trying to query the table with Raw SQL that AutoQuery generated:

    var sql = @"SELECT ""UV_Child"".""KindergartenId"", 
                        ""UV_Child"".""KindergartenName"", 
                        ""UV_Child"".""ClassId"", 
                        ""UV_Child"".""ClassName"", 
                        ""UV_Child"".""ChildId"", 
                        ""UV_Child"".""ChildName"", 
                        ""UV_Child"".""ParentId"", 
                        ""UV_Child"".""DisplayName"", 
                        ""UV_Child"".""UserName"", 
                        ""UV_Child"".""PhoneNumber"", 
                        ""UV_Child"".""ImageName"" 
                FROM ""UV_Child""
                WHERE ""UV_Child"".""ClassId"" = 112
                LIMIT 100";

    using (var db = HostContext.Resolve<IDbConnectionFactory>().Open())
    {
        var results = db.Select<UV_Child>(sql);
        results.PrintDump();
    }

AutoQuery generates the correct SQL here and after removing the reference to "UV_Child"."PhoneNumber" the query works!

To make sure it’s not an issue with OrmLite I’ve created a new test project using a new Sqlite database that’s created with a new table which I can verify the schema is created with the table.

[TestFixture]
public class StandAloneTests
{
    private IDbConnectionFactory dbFactory;

    public StandAloneTests()
    {
        this.dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
    }

    [Test]
    public void Query_children_by_teacher_test()
    {
        var sql = @"SELECT ""UV_Child"".""KindergartenId"", 
                        ""UV_Child"".""KindergartenName"", 
                        ""UV_Child"".""ClassId"", 
                        ""UV_Child"".""ClassName"", 
                        ""UV_Child"".""ChildId"", 
                        ""UV_Child"".""ChildName"", 
                        ""UV_Child"".""ParentId"", 
                        ""UV_Child"".""DisplayName"", 
                        ""UV_Child"".""UserName"", 
                        ""UV_Child"".""PhoneNumber"", 
                        ""UV_Child"".""ImageName"" 
                FROM ""UV_Child""
                WHERE ""UV_Child"".""ClassId"" = 112
                LIMIT 100";

        using (var db = dbFactory.Open())
        {
            db.DropAndCreateTable<UV_Child>();

            var results = db.Select<UV_Child>(sql);
            results.PrintDump();
        }
    }
}

And this works as well! So after spending hours trying to track down this issue it doesn’t appear to be an issue with AutoQuery or OrmLite. I don’t know what the issue is with your Sqlite database, but I suspect your database may be corrupted.

I’ve also no idea how you managed to even build your solution with classes that containing illegal characters like this:

[Alias("UV_Stats_PageReportMaster")]
public partial class UV_Stats_PageReportMaster 
{
    [AutoIncrement]
    public int MasterId { get; set;}
    public int? PageReportType { get; set;}
    public string ±»·ÃÎʹ¦ÄÜ { get; set;}
    public int? ·ÃÎÊ×ÜÊý { get; set;}
}

But to save me wasted hours in future, I’m going to require any future issues to include an isolated, stand-alone repro of the issue. Not your entire solution, not an existing database - an isolated repro that compiles, that I can run, that only has the minimal code to demonstrate that it is an issue with the ServiceStack libraries and not any external state or environment issues.


Unrelated to this issue please also read the OrmLite limitations where every Table needs a single Primary Key, i.e. UV_Child doesn’t have one, you can add one with the [PrimaryKey] attribute.

Thank you very much, you answer help me a lot.

  1. I can build with Unicode characters because i use Chinese Visual Studio.
  2. I really should check log, sorry to waster your time.
  3. UV_Child is a view,The POCO is generated by T4, do not have [PrimaryKey]
  4. I want to use memory db to test, but it’s boring to insert initial data and delete them. But i know migrate data from sqlserver is not a good idea (with a lot of issues), i will update my test later, thank you again.
  5. After add UV_Child.PhoneNumber to UV_Child, i still can’t get right results of Query_questions_by_master_test, Query_questions_by_parent_test, Query_questions_by_teacher_test. But I have switch my test environment to SqlServer, So let it be. I will check my sqlite environment later.