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

Support multiple primary keys in schema #280

Open
mfeingol opened this issue Apr 23, 2014 · 13 comments
Open

Support multiple primary keys in schema #280

mfeingol opened this issue Apr 23, 2014 · 13 comments
Assignees
Labels

Comments

@mfeingol
Copy link

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 ";
            }
@buehler
Copy link

buehler commented Apr 24, 2014

Please create a pullrequest for this! this is awesome.

@mfeingol
Copy link
Author

Done. Pull request is #281.

@buehler
Copy link

buehler commented Apr 25, 2014

thank you :)

@pkl728
Copy link

pkl728 commented Jul 27, 2015

Has this not been merged in yet?

@DmitriyYukhanov
Copy link

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

@andre-paulo98
Copy link

Any updates on this? @praeclarum

@mfeingol
Copy link
Author

mfeingol commented Jul 22, 2019

Wow, this is old.

A note: I am 100% sure that there are issues with the PR as it was submitted. It should be very carefully tested before proceeding.

@fredericoregateiro
Copy link

Without this my work becomes more complicated 😄
Can this feature make the next release?

@picolino
Copy link

Upvote

3 similar comments
@ruben-itinera
Copy link

Upvote

@BigBadBleuCheese
Copy link

Upvote

@ajitd
Copy link

ajitd commented May 4, 2020

Upvote

@alkee
Copy link

alkee commented Feb 25, 2022

Upvote from futrue. @praeclarum

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

No branches or pull requests