Modelling Linked List with OneToOne

Issue #82 resolved
Craig Jacobs created an issue

I am trying to save data that will model a linked list. Each item inserted into the table will contain a ForeignKey reference to another item in the same table. In my particular case, I want each item to retain a link to the previous item in the list.

The data seem to insert properly but when I try to read the data back out, the Foreign Keys point to the wrong items.

Is this type of data model even possible with SQLite-Net Extensions. I know there has been an effort made to keep the capabilities focused for reasons of compatibility and speed.

I've attached a small program that demonstrates my question.

Thanks, in advance, for the guidance. And thanks for a great library.

Comments (3)

  1. Guillermo GutiƩrrez

    The issue seems to be related with inverse relationship discovery (#17). Right now, it will try to find the inverse relationship in the opposite class to update it and it will find exactly the same property in the same class, and will override it.

    Try setting the inverse relationship to "" to disable automatic discovery and see if that fixes the issue.

    public class LinkedListItem
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
    
        public string Name { get; set; }
    
        public int PreviousItemId { get; set; }
    
        [OneToOne(nameof(PreviousItemId), CascadeOperations = CascadeOperation.CascadeRead), InverseProperty = ""]
        public LinkedListItem PreviousItem { get; set; }
    }
    
  2. Craig Jacobs reporter

    Brilliant!

    That fixes it for both the test program and for my actual project (with a much more complicated data model).

    Thanks for the quick reply.

  3. Log in to comment