Forum Cross Platform with Xamarin

SQLite-net-pcl - SQLite.SQLiteException: Cannot add a PRIMARY KEY column

I seem to be hitting all sorts of problems today. I simply cannot get Sqlite-net-pcl to behave correctly when signing an APK. Debug and release work fine but everytime I create a signed version of the APK the app crashes with this issue:-

SQLite.SQLiteException: Cannot add a PRIMARY KEY column

Here is an example of my model:-

using SQLite.Net.Attributes;
using System;

namespace App.BusinessLayer.LocalObjects
[Preserve(AllMembers = true)]
public class LocalTip

    public int ID { get; set; }

    [PrimaryKey, Column("tipObjectId")]
    public string objectId { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public string Title { get; set; }
    public string Bet { get; set; }
    public string Description { get; set; }
    public string Rating { get; set; }
    public string Time { get; set; }
    public string Bookies { get; set; }
    public string Url { get; set; }
    public int Status { get; set; }
    public Double Odds { get; set; }
    public int Outcome { get; set; }

    public LocalTip()
        this.objectId = " ";
        this.Title = " ";
        this.Bet = " ";
        this.Description = " ";
        this.Rating = " ";
        this.Time = " ";
        this.Bookies = " ";
        this.Url = " ";


Has anyone seen this before?


  • PhillWigginsPhillWiggins USMember

    Strange thing is the app crashes when it loads with the above issue. If you go to Applications and Clear Data for this app, it works perfect then?

  • Hi,
    Probably it´s happen because you have any change in your table.
    If you change the primary key, for example, the CreateTable


    table>(); method executes if the table doesn't exists.

  • TheSicariusTheSicarius Member

    What an issue i was having with this.... hours... gone from my life.

    Long story short for my solution, i found out through various locations on the web that you can't edit a SQLite DB table class having already ran the app and had the table created previously.

    After copying and pasting out the new code that made this error pop up for me to the point where my DB table class was back to its former glory when i ran the app for the first time i was able to run a quick line of code to "Drop Table" which looked something like this:

    public void DeleteWorkShiftTable()


    After i dropped the WorkShift Table, i was able to go back into my WorkShift Table Class and Add the Primary Key / Auto-Increment aspect that i found out i needed for my DB to properly work. That looked a little like this:

    public class WorkShift : BaseWorkShift
    public int WorkShiftID { get; set; }
    public DateTime ShiftDate { get; set; }
    public int Mileage { get; set; }
    public int Expenses { get; set; }
    public int Earnings { get; set; }
    public int Hours { get; set; }
    public WorkShift()

    public abstract class BaseWorkShift
        [PrimaryKey, AutoIncrement]
        public int PrimaryKey { get; set; }


    Hope this helps, Tried to be as explicit as i could. Searching the web always pays off for me, its just a matter of how many hours am i willing to dump at it..

    Best of Luck!

  • JohnBlack.2526JohnBlack.2526 USMember ✭✭

    Official IOU for the sole use of TheSicarius

    Can be redeemed for one beer

    Hi TheSicarius,

    I know it may not mean much but your lost hours has probably saved me a morning of head-scratching.


  • litov74litov74 Member

    Well, TheSicarius was right.
    I had the same issue with Redmi 5 but this issue disappears when I deploy to a virtual device.

    Here is my snippet with solution:

    public UserDatabaseController()
                database = DependencyService.Get<ISQLite>().GetConnection();
                //uncomment this line and re-deploy the solution if the "Primary key error" returns.
Sign In or Register to comment.