Support multiple primary keys in schema
See original GitHub issueHere’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:
- Created 9 years ago
- Comments:13
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any news on this? Would be great to have multicolumn primary keys!
Upvote from futrue. @praeclarum