Skip to content

Support multiple primary keys in schema #280

Description

@mfeingol

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 ";
            }

Activity

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions