Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite ORM primary keys on multiple columns

What is the syntax for specifying a primary key on more than 1 column in SQLite?

I'm developing an app in C# using Xamarin and would like to use tables with a primary key on mulitple columns using SQLite ORM.

The TableObject class defines a table. I would like to make sure the combination of ProductID and RetailerID is unique. To accomplish this, I want to make the combination of columns the primary key.

I've tried the following:

class TableObject
{
    [PrimaryKey]
    public int ProductID { get; set; }
    [PrimaryKey]
    public int RetailerID { get; set; }
    public decimal Price { get; set; }
}

The table is created like this:

database.CreateTable<Cache>();

But I get the following exception:

SQLite.Net.SQLiteException: table "TableObject" has more than one primary key

In 'default' SQLite this can be done using:

CREATE TABLE something (column1, column2, column3, PRIMARY KEY (column1, column2));

But how do I accomplish this using SQLite ORM?

like image 761
Devin19 Avatar asked Sep 04 '26 01:09

Devin19


1 Answers

This is not supported as you can read here: https://forums.xamarin.com/discussion/14769/how-do-we-create-a-composite-primary-key-using-sqlite-net-orm

Some one has extended SQLite PCL to enable some additional features https://github.com/softlion/SQLite.Net-PCL

like image 192
Francesco Avatar answered Sep 05 '26 14:09

Francesco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!