Search code examples
haskellroutesyesodpersistenthaskell-persistent

How can I use a field from my Persistent model as a path piece in my routes file?


I have a Persistent model like so:

Organization sql=organizations
    Id UUID default=uuid_generate_v1mc
    name Text
    UniqueOrganizationName name
    deriving Show Eq Typeable

I'd like to use the organization name in my Yesod routes file like so:

/#OrganizationName/onboarding/business/about OnboardingBusinessAboutR POST

Using OrganizationName or UniqueOrganizationName there gives me this error:

/Users/maximiliantagher/Documents/Mercury/hs/mercury-web-backend/src/Foundation.hs:41:1: error:
    • Data constructor ‘OrganizationName’ cannot be used here
        (Perhaps you intended to use TypeInType)
    • In the type ‘OrganizationName’
      In the definition of data constructor ‘OnboardingBusinessAboutR’
      In the data instance declaration for ‘Route’

I could use a newtype as a workaround, just wondered if that was necessary.

I imagine the lack of a PathPiece instance is a problem, but if it was just that then the error would be No instance for (PathPiece ...).

Using either OrganizationName or UniqueOrganizationName would be fine.


Solution

  • The OrganizationName type is a constructor, because this is what is used in Persistent machinery like selectList. In reality, name is a parameter of the Organization constructor of type Text, so you just need #Text in your route.