Search code examples
androidsqlitexamarinsqlite-netsqlite-net-extensions

SQLite Extensions exception: ManyToOne relationship destination must have Primary Key


You can see the error in title. There are my table classes:

public class Cars : Table
{
    [NotNull]
    public string name { get; set; }
    [ForeignKey(typeof(Models)), NotNull]
    public int model_out_id { get; set; }
    [ForeignKey(typeof(Bodies)), NotNull]
    public int body_out_id { get; set; }
    [MaxLength(50)]
    public string vin { get; set; }
    [MaxLength(4), NotNull]
    public int year { get; set; }
    [Indexed, NotNull]
    public long created_at { get; set; } = DateTime.Now.GetTimestamp();

    [ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public Models Model { get; set; }
    [ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public Bodies Body { get; set; }

    [OneToMany]
    public List<CarGlasses> CarGlasses { get; set; }

    public Cars()
    {
    }
}

public class Models : TableLibrary
{
    [PrimaryKey, NotNull]
    public int out_id { get; set; }
    [NotNull]
    public string name { get; set; }
    [ForeignKey(typeof(Marks)), NotNull]
    public int mark_out_id { get; set; }
    [Indexed, NotNull]
    public int year_from { get; set; }
    [Indexed]
    public int? year_to { get; set; }

    [ManyToOne(CascadeOperations = CascadeOperation.CascadeRead), Ignore]
    public Marks Mark { get; set; }

    public Models()
    {
    }
}

Error occurs here:

inst.GetChild(car, "Model");

inst is SQLite.Net.SQLiteConnection instance

Everything worked fine when I used library as plain code, but now I added it as PCL reference. As you can see PrimaryKey exists in Models table. What's wrong with this code?


Solution

  • Here is my thread on xamarin forums. I've got solution there.

    https://forums.xamarin.com/discussion/comment/288455