From d8c45fe1bedcc15e292c735b8f2ee942505a8e55 Mon Sep 17 00:00:00 2001 From: MoonlightSentinel Date: Fri, 7 May 2021 22:12:52 +0200 Subject: [PATCH] database_generation: Don't require an id attribute for insert --- database_generation.d | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/database_generation.d b/database_generation.d index 6c4470e..0c80c65 100644 --- a/database_generation.d +++ b/database_generation.d @@ -341,11 +341,28 @@ void insert(O)(ref O t, Database db) { foreach(row; db.query("SELECT max(id) FROM " ~ toTableName(O.stringof))) t.id.value = to!int(row[0]); } else { - foreach(row; builder.execute(db, "RETURNING id")) // FIXME: postgres-ism - t.id.value = to!int(row[0]); + static if (__traits(hasMember, O, "id")) + { + foreach(row; builder.execute(db, "RETURNING id")) // FIXME: postgres-ism + t.id.value = to!int(row[0]); + } + else + { + builder.execute(db); + } } } +// Check that insert doesn't require an `id` +unittest +{ + static struct NoPK + { + int a; + } + + alias test = insert!NoPK; +} /// class RecordNotFoundException : Exception { this() { super("RecordNotFoundException"); }