Search code examples
sqldatabasehaskelltuplespersist

Haskell : Using Tuple in a Persist.Sql - Database as PersistSqlField


I have created a database in which I would like to have a Field of the type [(String, [String])].

    share [mkPersist sqlSettings, mkMigrate "migrateAll"][persistLowerCase|
    ConfigDB
        numberOfParticipants Int
        groupId              Int
        memberList           [(String,[String])]
        deriving Show
    |]

However, I get an error message:

Exception when trying to run compile-time code: Invalid field type >"[(String,[String])]" PSFail PSFail "PSFail \"(\\"\\",\\"\\",Nothing)\"

The error seems to arise from using a tuple, because [String] works just fine, but if I write (String, String) the compiler tells me:

"Not in scope: type constructor or class String, Perhaps you meant String (imported from Prelude)"

In Database.Persist.Sql I find (PersistFieldSql a, PersistFieldSql b) => PersistFieldSql (a, b) is an instance, so it should work.

Also, importing Data.Tuple didn't help. I have imported Database.Persist.Sqlite, of course.

Thanks in advance,

Sophia


Solution

  • It's tricky (and sometimes impossible) to embed complex types inside the persistent definition syntax. My recommendation is to define a type synonym in your Haskell code, e.g.:

    type MemberList = [(String, [String])]
    

    and then use that type inside the definition.