diff --git a/IHP/SchemaCompiler.hs b/IHP/SchemaCompiler.hs index dfd521681..12df92462 100644 --- a/IHP/SchemaCompiler.hs +++ b/IHP/SchemaCompiler.hs @@ -739,7 +739,7 @@ instance QueryBuilder.FilterPrimaryKey "#{name}" where where primaryKeyPattern = case primaryKeyColumns table of [] -> error $ "Impossible happened in compilePrimaryKeyInstance. No primary keys found for table " <> cs name <> ". At least one primary key is required." - [c] -> c.name + [c] -> columnNameToFieldName c.name cs -> "(Id (" <> intercalate ", " (map (columnNameToFieldName . (.name)) cs) <> "))" primaryKeyFilters :: [Text] diff --git a/Test/SchemaCompilerSpec.hs b/Test/SchemaCompilerSpec.hs index 704eff7bd..e90af02e0 100644 --- a/Test/SchemaCompilerSpec.hs +++ b/Test/SchemaCompilerSpec.hs @@ -442,7 +442,55 @@ tests = do builder |> QueryBuilder.filterWhere (#id, id) {-# INLINE filterWhereId #-} |] - + describe "compileFilterPrimaryKeyInstance" do + it "should compile FilterPrimaryKey instance when primary key is called id" do + let statement = StatementCreateTable $ CreateTable { + name = "things", + columns = [ Column "id" PUUID Nothing False False Nothing ], + primaryKeyConstraint = PrimaryKeyConstraint ["id"], + constraints = [], + unlogged = False + } + let compileOutput = compileStatementPreview [statement] statement |> Text.strip + + getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming| + instance QueryBuilder.FilterPrimaryKey "things" where + filterWhereId id builder = + builder |> QueryBuilder.filterWhere (#id, id) + {-# INLINE filterWhereId #-} + |] + it "should compile FilterPrimaryKey instance when primary key is called thing_id" do + let statement = StatementCreateTable $ CreateTable { + name = "things", + columns = [ Column "thing_id" PUUID Nothing False False Nothing ], + primaryKeyConstraint = PrimaryKeyConstraint ["thing_id"], + constraints = [], + unlogged = False + } + let compileOutput = compileStatementPreview [statement] statement |> Text.strip + + getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming| + instance QueryBuilder.FilterPrimaryKey "things" where + filterWhereId thingId builder = + builder |> QueryBuilder.filterWhere (#thingId, thingId) + {-# INLINE filterWhereId #-} + |] + it "should compile FilterPrimaryKey instance when primary key is composite of thing_id other_id" do + let statement = StatementCreateTable $ CreateTable { + name = "thing_other_rels", + columns = [ Column "thing_id" PUUID Nothing False False Nothing, Column "other_id" PUUID Nothing False False Nothing], + primaryKeyConstraint = PrimaryKeyConstraint ["thing_id", "other_id"], + constraints = [], + unlogged = False + } + let compileOutput = compileStatementPreview [statement] statement |> Text.strip + + getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming| + instance QueryBuilder.FilterPrimaryKey "thing_other_rels" where + filterWhereId (Id (thingId, otherId)) builder = + builder |> QueryBuilder.filterWhere (#thingId, thingId) |> QueryBuilder.filterWhere (#otherId, otherId) + {-# INLINE filterWhereId #-} + |] getInstanceDecl :: Text -> Text -> Text getInstanceDecl instanceName full = @@ -460,4 +508,4 @@ getInstanceDecl instanceName full = takeInstanceDecl (line:rest) | isEmpty line = [] | otherwise = line : takeInstanceDecl rest - takeInstanceDecl [] = error "never encountered newline?" + takeInstanceDecl [] = [] -- EOF reached