Skip to content

Lens example in tutorial does not work #664

Open
@tdimiduk

Description

@tdimiduk

The lens generation example in the docs appears to be malformed. UserId seems to be used as a data constructor in the pattern match, but it is not one:

Prelude> :load Tutorial.hs 
[1 of 1] Compiling Main             ( Tutorial.hs, interpreted )

Tutorial.hs:39:10: error:
    Not in scope: data constructor ‘UserId’
    Perhaps you meant ‘User’ (line 8)
   |
39 |         (UserId (LensFor addressForUserId)) =

Where I have made a file called Tutorial.hs that pulls in the relevant lines from tutorials 1 and 2

Tutorial.hs

{-#  LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
import Database.Beam
import GHC.Int
import Data.Text (Text)

data UserT f
    = User
    { _userEmail     :: Columnar f Text
    , _userFirstName :: Columnar f Text
    , _userLastName  :: Columnar f Text
    , _userPassword  :: Columnar f Text }
    deriving Generic
type User = UserT Identity
type UserId = PrimaryKey UserT Identity

data AddressT f = Address
                { _addressId    :: C f Int32
                , _addressLine1 :: C f Text
                , _addressLine2 :: C f (Maybe Text)
                , _addressCity  :: C f Text
                , _addressState :: C f Text
                , _addressZip   :: C f Text
                , _addressForUser :: PrimaryKey UserT f }
                  deriving (Generic, Beamable)

type Address = AddressT Identity
deriving instance Show (PrimaryKey UserT Identity)
deriving instance Show Address

instance Table AddressT where
    data PrimaryKey AddressT f = AddressId (Columnar f Int32) deriving (Generic, Beamable)
    primaryKey = AddressId . _addressId
type AddressId = PrimaryKey AddressT Identity -- For convenience

Address (LensFor addressId)    (LensFor addressLine1)
        (LensFor addressLine2) (LensFor addressCity)
        (LensFor addressState) (LensFor addressZip)
        (UserId (LensFor addressForUserId)) =
        tableLenses

User (LensFor userEmail)    (LensFor userFirstName)
     (LensFor userLastName) (LensFor userPassword) =
     tableLenses

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions