SQLite.Net.SQLiteException: Constraint on InsertAsync

Issue #115 resolved
Emil Alipiev created an issue

I have a simple insert query as below

await db_async.InsertAsync(new ClasssStudentMapping() { Classsid = item.id, Studentid = Student.id });

Both values Classsid and Studentid are given and they are unique. I mean they arent exist in the sqlite table (I have already verified).

Despite of that I get exception as below, my class definition is also below. Can you please help me? why do I get this exception.

{

public   class ClasssStudentMapping
    {
        [PrimaryKey]
        public int id { get; set; }

        [ForeignKey(typeof(Database.Model.Classs)), NotNull]
        public int Classsid { get; set; }

        [ForeignKey(typeof(Database.Model.Student)), NotNull]
        public int Studentid { get; set; }



    }
SQLite.Net.SQLiteException: Constraint
  at SQLite.Net.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x0016b] in <8f2bb39aeff94a30a8628064be9c7efe>:0 
  at SQLite.Net.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x000bc] in <8f2bb39aeff94a30a8628064be9c7efe>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 

Comments (4)

  1. Guillermo GutiƩrrez

    Have you tried adding AutoIncrement attribute to id property?

        [PrimaryKey, AutoIncrement]
        public int id { get; set; }
    
  2. Log in to comment