SQLite.Net.SQLiteException: near "where": syntax error

Issue #91 closed
Jeremy Connor created an issue

When attempting to use the ManyToMany relationship with the following code I get a rather nondescript error on conn.Update(element); in WriteOperations.cs:UpdateWithChildren(SQLiteConnection, object).

The error is: SQLite.Net.SQLiteException: near "where": syntax error

My code is:

    [Table("Contact")]
    public class Contact
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [MaxLength(50)]
        public string FirstName { get; set; }

        [MaxLength(50)]
        public string Surname { get; set; }

        [ManyToMany(typeof(Participant))]
        public List<Journey> Journeys { get; set; }

    }

    public class Participant
    {
        [ForeignKey(typeof(Contact))]
        public int ContactId { get; set; }

        [ForeignKey(typeof(Journey))]
        public int JourneyId { get; set; }
    }

    [Table("Journey")]
    public class Journey
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [ManyToMany(typeof(Participant))]
        public List<Contact> Contacts { get; set; }

    }




string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "xandroid.db3");

var db = new SQLiteConnection(platform, dbPath);

db.DropTable<Contact>();
db.DropTable<Participant>();
db.DropTable<Journey>();
db.CreateTable<Contact>();
db.CreateTable<Participant>();
db.CreateTable<Journey>();

Contact beans = new Contact { FirstName = "Beans", Surname = "Connor" };
Contact winston = new Contact { FirstName = "Winston", Surname = "Connor" };
var contacts = new List<Contact> { beans, winston };
db.InsertAllWithChildren(contacts);

Journey returnHome = new Journey { Contacts = contacts };
db.InsertAllWithChildren(new List<Journey> { returnHome }, recursive: true);

The error occurs on the final call to db.InsertAllWithChildren for the Journey objects.

Comments (5)

  1. Guillermo Gutiérrez

    I was able to reproduce this error with this code:

    Journey returnHome = new Journey();
    db.Insert(returnHome);
    db.Update(returnHome);
    

    It seems that sqlite-net library (and not this project) doesn't handle well entities with no other fields apart of the primary key. To fix it, just add any property to the Journey class:

    [Table("Journey")]
    public class Journey
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
    
        [ManyToMany(typeof(Participant))]
        public List<Contact> Contacts { get; set; }
    
        public string foo { get; set; }
    }
    
  2. Log in to comment