question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support multiple primary keys in schema

See original GitHub issue

Here’s a patch for that:

@@ -362,14 +362,21 @@ namespace SQLite
            if (!_tables.TryGetValue (ty.FullName, out map)) {
                map = GetMapping (ty, createFlags);
                _tables.Add (ty.FullName, map);
            }
            var query = "create table if not exists \"" + map.TableName + "\"(\n";
-           
-           var decls = map.Columns.Select (p => Orm.SqlDecl (p, StoreDateTimeAsTicks));
+
+      IEnumerable<TableMapping.Column> pks = from c in map.Columns where c.IsPK select c;
+      int numPKs = pks.Count();
+
+      var decls = map.Columns.Select(p => Orm.SqlDecl(p, StoreDateTimeAsTicks, numPKs == 1));
            var decl = string.Join (",\n", decls.ToArray ());
            query += decl;
+
+      if (numPKs > 1)
+        query += String.Format(",\nprimary key ({0})\n", string.Join(", ", pks.Select(c => "\"" + c.Name + "\"")));
+
            query += ")";

            var count = Execute (query);

            if (count == 0) { //Possible bug: This always seems to return 0?
@@ -1838,15 +1845,15 @@ namespace SQLite
    {
         public const int DefaultMaxStringLength = 140;
         public const string ImplicitPkName = "Id";
         public const string ImplicitIndexSuffix = "Id";

-       public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks)
+       public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks, bool setPK = true)
        {
            string decl = "\"" + p.Name + "\" " + SqlType (p, storeDateTimeAsTicks) + " ";

-           if (p.IsPK) {
+           if (setPK && p.IsPK) {
                decl += "primary key ";
            }
            if (p.IsAutoInc) {
                decl += "autoincrement ";
            }

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:13

github_iconTop GitHub Comments

5reactions
DmitriyYukhanovcommented, Mar 14, 2018

Any news on this? Would be great to have multicolumn primary keys!

0reactions
alkeecommented, Feb 25, 2022

Upvote from futrue. @praeclarum

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I have multiple primary keys in a single table?
Having two primary keys at the same time, is not possible. But (assuming that you have not messed the case up with composite...
Read more >
Can Multiple Primary Keys Exist on a Single Table?
The short answer is no, a table is not allowed to contain multiple primary keys , as that goes against the fundamental principles...
Read more >
Multiple primary keys in ecto - Questions / Help - Elixir Forum
In the migration we can add multiple primary keys they all act as composite primary keys. I have four fields in the table...
Read more >
Are multiple Primary Keys in MySQL allowed?
Only one primary key per table is permitted. But a primary key can consist of any number of columns. Having many columns in...
Read more >
Can there be multiple primary keys with examples?
Yes there can be more than one primary key's i.e. “a composite key”. One may define a single column as primary key while...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found