Skip to content

Commit

Permalink
Fixed has many instance issue with nullable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 18, 2020
1 parent 9151985 commit a81a475
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,22 @@ compileFromRowInstance table@(CreateTable { name, columns }) =
compileField (fieldName, _) | isManyToManyField fieldName = let (Just ref) = find (\(n, _) -> columnNameToFieldName n == fieldName) referencing in compileSetQueryBuilder ref
compileField _ = "pure def"

compileSetQueryBuilder (refTableName, refFieldName) = "pure (QueryBuilder.filterWhere (Data.Proxy.Proxy @" <> tshow (columnNameToFieldName refFieldName) <> ", id) (QueryBuilder.query @" <> tableNameToModelName refTableName <> "))"
compileSetQueryBuilder (refTableName, refFieldName) = "pure (QueryBuilder.filterWhere (Data.Proxy.Proxy @" <> tshow (columnNameToFieldName refFieldName) <> ", " <> primaryKeyField <> ") (QueryBuilder.query @" <> tableNameToModelName refTableName <> "))"
where
-- | When the referenced column is nullable, we have to wrap the @Id@ in @Just@
primaryKeyField :: Text
primaryKeyField = if get #notNull refColumn then "id" else "Just id"

(Just refTable) = let (Schema statements) = ?schema in
statements
|> find \case
CreateTable { name } -> name == refTableName
otherwise -> False

refColumn :: Column
(Just refColumn) = refTable
|> \case CreateTable { columns } -> columns
|> find (\col -> get #name col == refFieldName)


compileQuery column@(Column { name }) = columnNameToFieldName name <> " = (" <> toBinding modelName column <> ")"
Expand Down

0 comments on commit a81a475

Please sign in to comment.