Getting null on sub children

Issue #142 new
Adam Richardson created an issue

Working on an app using SQLite-net-pcl and SQLiteNetExtensions and I’m having a problem with getting data from a sub child table

Following is the code I have for the tables:

using SQLite;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.ObjectModel;

namespace ACNLTracker
{
    public class Daily
    {
        [PrimaryKey]
        public DateTime Date { get; set; }
        //Other data that doesn't pertain to this question

        [OneToMany(CascadeOperations = CascadeOperation.All)]
        public ObservableCollection<Resident> Residents { get; set; }
    }

    [Table(&#34;residents&#34;)]
    public class Resident
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public int ResidentID { get; set; }

        [ForeignKey(typeof(Villager))]
        public int VillagerID { get; set; }

        [ForeignKey(typeof(Daily))]
        public DateTime DailyDate { get; set; }

        [OneToOne(CascadeOperations = CascadeOperation.CascadeRead, ReadOnly = true)]
        public Villager Villager { get; set; }
    }

    [Table(<Villagers>)]
    public class Villager
    {
        [PrimaryKey]
        public int ID { get; set; }
        public string Name { get; set; }
        public string Species { get; set; }
        public int Gender { get; set; }
        public string Birthday { get; set; }
        public string Image { get; set; }
    }
}

Here are two of the calls I make for testing:

public Daily GetToday() => db.GetAllWithChildren<Daily>(recursive: true).Where(x =&gt; x.Date == DateTime.Today).FirstOrDefault(); 

public ObservableCollection<Resident> GetResidents() => db.GetAllWithChildren<Resident>(recursive: true).ToObservableCollection();

If I use:

Daily daily = App.Database.GetToday();

I get all of Today's Daily data and the Resident data associated, but Villager under Resident remains null.

However

Resident[] resident = App.Database.GetResidents();

Returns all the residents and the Villager table is not null. Is it not possible to get sub child tables using this method, or is this a bug?

Note: The version to select only goes to 1.2.0 however I use version 2.1.0 from NuGet.

Comments (2)

  1. Guillermo Gutiérrez

    Can you try using a primary key that’s not a DateTime? Maybe that’s probably causing the problem.

    Lastly, not related to the issue, but you can use the filter parameter to perform the query before obtaining the relationships. It’s way more efficient than doing it afterwards:

    db.GetAllWithChildren<Daily>(x => x.Date == DateTime.Today, recursive: true).FirstOrDefault();

  2. Log in to comment