首页 > 解决方案 > 在没有许多的地方选择一些

问题描述

我正在尝试查找没有任何具有给定名称的用户名的域,即免费域。希望有一个不需要2个单独查询的解决方案,但我也遇到了类型系统的问题。

-- error Couldn't match type 'IO [Domain]' with '[[Domain]]', points to beginning of filter

            Just term -> do
                usernames <- query @Username
                    |> filterWhere (#name, term) |> fetch >>= collectionFetchRelated #domainId
                domains <- search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
                    Just _ -> True
                    Nothing -> False
                render IndexView {..}

标签: ihp

解决方案


-- error Couldn't match type 'IO [Domain]' with '[[Domain]]', points to beginning of filter由于此处的箭头符号而发生错误:

                domains <- search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
                    Just _ -> True
                    Nothing -> False

由于这没有做任何 IO,它需要使用let

                let domains = search |> filter \x->case find (\y->get #domainId x == get #domainId y) usernames of
                    Just _ -> True
                    Nothing -> False

现在的类型domains[[Domain]](域列表列表)。可能我们想要[Domain](只是一个普通的域列表)。

我们可以concat为此目的使用:

                let domains = search
                        |> filter ( \x-> case find (\y->get #domainId x == get #domainId y) usernames of
                                Just _ -> True
                                 Nothing -> False
                            )
                        |> concat

现在类型错误应该消失了。


希望有一个不需要2个单独查询的解决方案

是的,这也适用于手写查询:

domains <- sqlQuery "SELECT domains.* FROM domains WHERE (SELECT COUNT(*) FROM usernames WHERE usernames.domain_id = domains.id AND usernames.name = ?) = 0" [term]

推荐阅读