Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary key support for more then one column - i.e. Composite key #134

Open
kamransaleem opened this issue Dec 12, 2012 · 13 comments
Open

Comments

@kamransaleem
Copy link

public class Image
{
[PrimaryKey]
public int ItemId { get; set; }

    [PrimaryKey]
    public int ImageId { get; set; }

    public string Src { get; set; }

    public int Width { get; set; }

    public int Height { get; set; }

    public byte[] ImageBinary { get; set; }
}

Get the following error:

SQLite.SQLiteException was unhandled by user code
HResult=-2146233088
Message=table "Image" has more than one primary key
Source=MyPocket
StackTrace:
at SQLite.SQLite3.Prepare2(IntPtr db, String query) in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 2392
at SQLite.SQLiteCommand.Prepare() in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 1699
at SQLite.SQLiteCommand.ExecuteNonQuery() in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 1573
at SQLite.SQLiteConnection.Execute(String query, Object[] args) in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 467
at SQLite.SQLiteConnection.CreateTable(Type ty) in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 318
at SQLite.SQLiteConnection.CreateTableT in c:\dev\tfs\MyPocket\MyPocket\SQLite.cs:line 288
at MyPocket.Data.SQLitePocketRepository..ctor(String dbPath) in c:\dev\tfs\MyPocket\MyPocket\Data\SQLitePocketRepository.cs:line 24
at MyPocket.View.HomePage.d__0.MoveNext() in c:\dev\tfs\MyPocket\MyPocket\View\HomePage.xaml.cs:line 112
InnerException:

@georgepiva
Copy link

As composite key are not supported, I would like to know if there is any work around for it or if there are some best practices to follow in order to handle situations where a composite key appears to be necessary.

Source:
http://stackoverflow.com/questions/11105487/sqlite-net-and-foreign-keys

@softlion
Copy link

i created a patch

@JonnyWideFoot
Copy link

@softlion - Can I ask where you put the patch - I'd love support for composite primary keys. Thanks, Jon

@softlion
Copy link

softlion commented May 3, 2013

#175

xperiandri pushed a commit to xperiandri/sqlite-net that referenced this issue Sep 12, 2015
@SeymourRu
Copy link

...well?
May be it`s a good time to fix it?

@softlion
Copy link

...well?
May be it`s a good time to fix it?

It’s fixed since 3 years in my rewrite there https://github.com/softlion/SQLite.Net-PCL2

@SeymourRu
Copy link

...well?
May be it`s a good time to fix it?

It’s fixed since 3 years in my rewrite there https://github.com/softlion/SQLite.Net-PCL2

Yeah, I`ve seen this, thank you!
But still this package is not fixed yet and this is relatively simple feature.

@softlion
Copy link

softlion commented May 25, 2021

What's fantastic, is that the task is marked as "up for grab".
Man, the pull request has 3 years now.
I won't update it anyway.

@flottokarotto
Copy link

Is there any chance to have this feature in the next 2 years?

@softlion
Copy link

softlion commented Aug 18, 2022 via email

@tentom
Copy link

tentom commented Oct 4, 2023

Adding composite key manually seems to work as a workaround for anyone driving here.

var cmd = connection.CreateCommand(
                "CREATE TABLE IF NOT EXISTS " + new TableMapping(typeof(ChatMessageDb)).TableName + " (" +
                " " + nameof(ChatMessageDb.ChatType) + " integer not null, " +
                " " + nameof(ChatMessageDb.Id) + " integer not null, " +
                " primary key (" + nameof(ChatMessageDb.ChatType) + ", " + nameof(ChatMessageDb.Id) + ")" +
                ");"
                );
            cmd.ExecuteNonQuery();

With the ChatMessageDb like this:

    public class DbRecord
    {
        public bool IsDeleted { get; set; }
        public DateTime UpdatedUtc { get; set; }
    }
    public class ChatMessageDb : DbRecord
    {
        public const short ChatType_Chat = 1;

        [PrimaryKey]
        public short ChatType { get; set; }
        [PrimaryKey]
        public int Id { get; set; }

        public int ContactUserId { get; set; }
        public bool IsSent { get; set; }
        public string Content { get; set; }
        public DateTime SentDateTime { get; set; }

    }

This was also mentioned in #1101

@softlion
Copy link

softlion commented Oct 4, 2023

@tentom why do that when a version that does natively support composite primary keys is available ?
https://github.com/softlion/SQLite.Net-PCL2

@tentom
Copy link

tentom commented Oct 4, 2023

Because it is a quick fix and workaround.

But I will take a look at that library as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants