lykahb / groundhog

This library maps datatypes to a relational model, in a way similar to what ORM libraries do in OOP. See the tutorial https://www.schoolofhaskell.com/user/lykahb/groundhog for introduction
http://hackage.haskell.org/package/groundhog
176 stars 39 forks source link

Sum types with common fields #77

Closed blaxill closed 6 years ago

blaxill commented 6 years ago

Sum types with common fields appear not to work, e.g.:

data Foo = A { bar :: Int } | B { bar :: Int, unique :: Int}

mkPersist defaultCodegenConfig [groundhog|
- entity: Foo
|]

When using the quasiquoting, it fails due to generating multiple bar field constructors:

Main.hs:26:1: error:
        Multiple declarations of ‘BarField’
        Declared at: Main.hs:26:1
                     Main.hs:26:1

Also using mkPersist without quasiquoting fails again due to duplicate constructors. I'd expect it to succeed and generate a schema sharing the common field for different constructors.

Is there away around this without modifying the data structure (which in my case is deep in a dependency)?

lykahb commented 6 years ago

You can change the name in the mapping declaration with exprName. https://hackage.haskell.org/package/groundhog-th-0.8.0.1/docs/Database-Groundhog-TH.html

blaxill commented 6 years ago

Thanks, I had seen the exprName but incorrectly assumed it modified the dbName.