beam icon indicating copy to clipboard operation
beam copied to clipboard

withDbModification won't work with single table

Open tommyengstrom opened this issue 4 years ago • 2 comments

It seems modifying the database doesn't work unless you have more than one table.

data MytableT f = Mytable
    { transactionPartnerName :: C f Int
    }
    deriving Generic

instance Beamable MytableT

instance Table MytableT where
    data PrimaryKey MytableT f = NoPKey deriving (Generic, Beamable)
    primaryKey _ = NoPKey

data MyDb f = MyDb
    { dbMyTable :: f (TableEntity MytableT)
    }
    deriving (Generic, Database Postgres)

myDb :: DatabaseSettings Postgres MyDb
myDb = defaultDbSettings
    `withDbModification` dbModification { dbMyTable = setEntityName "coolName" }

When I try to use withDbModification like this GHC is for some reason not able to figure out the backend.

    • Ambiguous type variable ‘be0’ arising from a use of ‘dbModification’
      prevents the constraint ‘(Database be0 MyDb)’ from being solved.
      Probable fix: use a type annotation to specify what ‘be0’ should be.
      These potential instance exist:
        instance Database Postgres MyDb
          -- Defined at ..........................
    • In the expression: dbModification
      In the second argument of ‘withDbModification’, namely
        ‘dbModification {dbMyTable = setEntityName "coolName"}’
      In the expression:
        defaultDbSettings
          `withDbModification`
            dbModification {dbMyTable = setEntityName "coolName"}
    |
    |     `withDbModification` dbModification { dbMyTable = setEntityName "coolName" }
    |         

But when I add another table like this

data MyDb f = MyDb
    { dbMyTable      :: f (TableEntity MytableT)
    , dbMyOtherTable :: f (TableEntity MytableT)
    }
    deriving (Generic, Database Postgres)

It all works fine.

Not sure if it's worth fixing, but a note in the docs would have been nice.

tommyengstrom avatar Oct 14 '21 14:10 tommyengstrom

Off the top of my head I'm not sure why the number of fields would matter, but you can work around it with a type application, e.g. (dbModification @_ @Postgres) { dbMyTable = setEntityName "coolName" }.

kmicklas avatar Oct 15 '21 01:10 kmicklas

Yeah maybe it's not an issue worth looking at. Just seemed very unexpected to me. It's fine to just close it.

tommyengstrom avatar Oct 18 '21 11:10 tommyengstrom