diff --git a/src/norm/model.nim b/src/norm/model.nim index 3c06a39f..f5309361 100644 --- a/src/norm/model.nim +++ b/src/norm/model.nim @@ -43,7 +43,7 @@ func table*(T: typedesc[Model]): string = func col*(T: typedesc[Model], fld: string): string = ## Get column name for a `Model`_ field, which is just the field name. - fld + "\"" & fld & "\"" func col*[T: Model](obj: T, fld: string): string = ## Get column name for a `Model`_ instance field. diff --git a/src/norm/postgres.nim b/src/norm/postgres.nim index ee54a617..72360c98 100644 --- a/src/norm/postgres.nim +++ b/src/norm/postgres.nim @@ -104,7 +104,7 @@ proc createTables*[T: Model](dbConn; obj: T) = uniqueGroupCols.add obj.col(fld) if val.isModel: - var fkGroup = "FOREIGN KEY($#) REFERENCES $#($#)" % + var fkGroup = """FOREIGN KEY($#) REFERENCES $#($#)""" % [obj.col(fld), typeof(get val.model).table, typeof(get val.model).col("id")] when obj.dot(fld).hasCustomPragma(onDelete): @@ -123,9 +123,9 @@ proc createTables*[T: Model](dbConn; obj: T) = const selfTableName = '"' & T.getCustomPragmaVal(tableName) & '"' else: const selfTableName = '"' & $T & '"' - fkGroups.add "FOREIGN KEY ($#) REFERENCES $#(id)" % [fld, selfTableName] + fkGroups.add """FOREIGN KEY ($#) REFERENCES $#(id)""" % [obj.col(fld), selfTableName] else: - fkGroups.add "FOREIGN KEY ($#) REFERENCES $#(id)" % [fld, (obj.dot(fld).getCustomPragmaVal(fk)).table] + fkGroups.add """FOREIGN KEY ($#) REFERENCES $#(id)""" % [obj.col(fld), (obj.dot(fld).getCustomPragmaVal(fk)).table] colGroups.add colShmParts.join(" ") diff --git a/tests/common/tmodel.nim b/tests/common/tmodel.nim index f72e806f..7b008edd 100644 --- a/tests/common/tmodel.nim +++ b/tests/common/tmodel.nim @@ -16,26 +16,26 @@ suite "Getting table and columns from Model": pet = newPet("cat", toy) person = newPerson("Alice", pet) - check person.col("name") == "name" - check pet.col("species") == "species" + check person.col("name") == "\"name\"" + check pet.col("species") == "\"species\"" - check person.cols == @["name", "pet"] - check person.cols(force = true) == @["name", "pet", "id"] + check person.cols == @["\"name\"", "\"pet\""] + check person.cols(force = true) == @["\"name\"", "\"pet\"", "\"id\""] - check person.fCol("name") == """"Person".name""" - check pet.fCol("species") == """"Pet".species""" + check person.fCol("name") == """"Person"."name"""" + check pet.fCol("species") == """"Pet"."species"""" check person.rfCols == @[ - """"Person".name""", - """"Person".pet""", - """"pet".species""", - """"pet".favToy""", - """"pet_favToy".price""", - """"pet_favToy".id""", - """"pet".id""", - """"Person".id""" + """"Person"."name"""", + """"Person"."pet"""", + """"pet"."species"""", + """"pet"."favToy"""", + """"pet_favToy"."price"""", + """"pet_favToy"."id"""", + """"pet"."id"""", + """"Person"."id"""" ] - check toy.rfCols == @[""""Toy".price""", """"Toy".id"""] + check toy.rfCols == @[""""Toy"."price"""", """"Toy"."id""""] test "Join groups": let @@ -44,8 +44,8 @@ suite "Getting table and columns from Model": person = newPerson("Alice", pet) check person.joinGroups == @[ - (""""Pet"""", """"pet"""", """"Person".pet""", """"pet".id"""), - (""""Toy"""", """"pet_favToy"""", """"pet".favToy""", """"pet_favToy".id""") + (""""Pet"""", """"pet"""", """"Person"."pet"""", """"pet"."id""""), + (""""Toy"""", """"pet_favToy"""", """"pet"."favToy"""", """"pet_favToy"."id"""") ] test "When related model has field with the type of the given model, expect name of that field as a string": diff --git a/tests/postgres/tdbtypes.nim b/tests/postgres/tdbtypes.nim index 7aa24187..04fd491b 100644 --- a/tests/postgres/tdbtypes.nim +++ b/tests/postgres/tdbtypes.nim @@ -4,7 +4,6 @@ import norm/[model, postgres, types] import ../models - const dbHost = "postgres" dbUser = "postgres" @@ -33,7 +32,7 @@ suite "Import dbTypes from norm/private/postgres/dbtypes": test "dbValue[DateTime] is imported": let users = @[newUser()].dup: - dbConn.select("""lastLogin <= $1""", ?now()) + dbConn.select(""""lastLogin" <= $1""", ?now()) check len(users) == 0 diff --git a/tests/postgres/tfkpragma.nim b/tests/postgres/tfkpragma.nim index ddc0be57..3e17a78c 100644 --- a/tests/postgres/tfkpragma.nim +++ b/tests/postgres/tfkpragma.nim @@ -4,7 +4,6 @@ import norm/[model, postgres] import ../models - const dbHost = "postgres" dbUser = "postgres" @@ -41,8 +40,8 @@ suite "``fk`` pragma": check customer.id > 0 let - userRows = dbConn.getAllRows(sql"""SELECT lastLogin, id FROM "User"""") - customerRows = dbConn.getAllRows(sql"""SELECT userId, email, id FROM "Customer"""") + userRows = dbConn.getAllRows(sql"""SELECT "lastLogin", "id" FROM "User" """) + customerRows = dbConn.getAllRows(sql"""SELECT "userId", "email", "id" FROM "Customer" """) check userRows.len == 1 check userRows[0][1] == ?user.id @@ -70,6 +69,6 @@ suite "``fk`` pragma": for inpCustomer in inpCustomers.mitems: dbConn.insert inpCustomer - dbConn.select(outCustomers, """"userid" = $1""", userA.id) + dbConn.select(outCustomers, """"userId" = $1""", userA.id) check outCustomers === inpCustomers[0..^2] diff --git a/tests/postgres/trows.nim b/tests/postgres/trows.nim index dacf3457..6c40e53f 100644 --- a/tests/postgres/trows.nim +++ b/tests/postgres/trows.nim @@ -1,5 +1,6 @@ import std/[unittest, with, strutils, sugar, options] + import norm/[model, postgres] import ../models @@ -67,7 +68,7 @@ suite "Row CRUD": let personRows = dbConn.getAllRows(sql"""SELECT name, pet, id FROM "Person"""") - petRows = dbConn.getAllRows(sql"""SELECT species, favToy, id FROM "Pet"""") + petRows = dbConn.getAllRows(sql"""SELECT species, "favToy", id FROM "Pet"""") toyRows = dbConn.getAllRows(sql"""SELECT price, id FROM "Toy"""") check personRows.len == 1 @@ -219,7 +220,7 @@ suite "Row CRUD": let personRow = get dbConn.getRow(sql"""SELECT name, pet, id FROM "Person" WHERE id = $1""", person.id) - petRow = get dbConn.getRow(sql"""SELECT species, favToy, id FROM "Pet" WHERE id = $1""", pet.id) + petRow = get dbConn.getRow(sql"""SELECT species, "favToy", id FROM "Pet" WHERE id = $1""", pet.id) toyRow = get dbConn.getRow(sql"""SELECT price, id FROM "Toy" WHERE id = $1""", pet.favToy.id) check personRow == @[?"Bob", ?pet.id, ?person.id] diff --git a/tests/postgres/ttables.nim b/tests/postgres/ttables.nim index 3f4bbc8a..1ebc8b86 100644 --- a/tests/postgres/ttables.nim +++ b/tests/postgres/ttables.nim @@ -56,7 +56,7 @@ suite "Table creation": check dbConn.getAllRows(qry, "FurnitureTable") == @[ @[?"id", ?"bigint"], - @[?"legcount", ?dftDbInt] + @[?"legCount", ?dftDbInt] ] test "Create tables": @@ -80,7 +80,7 @@ suite "Table creation": ] check dbConn.getAllRows(qry, "Pet") == @[ - @[?"favtoy", ?"bigint"], + @[?"favToy", ?"bigint"], @[?"id", ?"bigint"], @[?"species", ?"text"] ]