【发布时间】:2020-05-13 12:07:12
【问题描述】:
我在这样的Schema 模块中定义了DatasourceId
import qualified Database.Beam as B
data DatasourceT f = Datasource
{ _datasourceId :: B.C f Text
, _datasourceName :: B.C f Text
} deriving (Generic, B.Beamable)
instance B.Table DatasourceT where
data PrimaryKey DatasourceT f = DatasourceId (B.C f Text) deriving (Generic, B.Beamable)
primaryKey = DatasourceId . _datasourceId
type DatasourceId = B.PrimaryKey DatasourceT Identity
但我无法在 Schema 模块之外使用它
(B.val_ $ DatasourceId $ _datasourceId d)
因为编译器会抱怨
> • Data constructor not in scope:
> DatasourceId :: t0 -> B.PrimaryKey DatasourceT Identity
> • Perhaps you meant 'Datasource' (imported from Schema)
> |
> 229 | (B.val_ $ DatasourceId $ _datasourceId d)
虽然我有
import Schema (ControlAccessDb(..), Datasource, DatasourceId, DatasourceT(..))
那么有什么建议可以防止代码注定要被整合到一个模块中吗?这可能是类型家庭的怪癖吗? 看来问题出在
instance B.Table DatasourceT where
data PrimaryKey DatasourceT f = DatasourceId (B.C f Text) deriving (Generic, B.Beamable)
primaryKey = DatasourceId . _datasourceId
只有定义模块知道实例化内部。
请注意,Beam 文档本身也有这种用法(文档中嵌入的代码)。请参阅https://tathougies.github.io/beam/user-guide/manipulation/insert/,其中CustomerId 定义在https://github.com/tathougies/beam/blob/d87120b58373df53f075d92ce12037a98ca709ab/beam-sqlite/examples/Chinook/Schema.hs#L119
【问题讨论】:
标签: haskell haskell-beam