2017-05-30 8 views
0

Ich habe Match bekam folgendes:Typ konnte nicht ‚PersistEntityBackend (Entity a)‘ mit ‚SqlBackend‘

asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a 
asSqlBackendReader = id 

insertEnt :: (Entity a) -> IO (Key (Entity a)) 
insertEnt x = runWithDb $ do 
    insert $ x 
    where runWithDb = runSqlite "test.db" . asSqlBackendReader 

Der Zweck des asSqlBAckendReader-Persistent selectList causing error of "Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’" zurückzuführen ist.

Ich laufe in einem Fehler von:

• Couldn't match type ‘PersistEntityBackend (Entity a)’ 
       with ‘SqlBackend’ 
    arising from a use of ‘insert’ 
• In a stmt of a 'do' block: insert $ x 
    In the second argument of ‘($)’, namely ‘do { insert $ x }’ 
    In the expression: runWithDb $ do { insert $ x } 

Antwort

1

die Einschränkung auf die Unterschrift von insertEnt hinzufügen. Sie benötigen auch eine PersistEntity Einschränkung.

insertEnt 
    :: (PersistEntity (Entity a) 
    , PersistEntityBackend (Entity a) ~ SqlBackend) 
    => Entity a -> IO (Key (Entity a)) 

Um das (andere als nur geben dem Compiler, was es indirekt gefragt wird) ableiten, Sie bei the type of insert

insert 
    :: (PersistStoreWrite backend 
    , MonadIO m 
    , PersistRecordBackend record backend) 
    => record -> ReaderT backend m (Key record) 

Wir haben auch

type PersistRecordBackend record backend = 
    (PersistEntity record 
    , PersistEntityBackend record ~ BaseBackend backend) 

Außerdem aussehen, In Ihrer Anwendung haben Sie einige konkrete Typen:

Diese konkreten Typen
backend ~ SqlBackend 
m ~ (some concrete transformer stack on top of IO) 
record ~ Entity a 

entladen PersistStoreWrite backend und MonadIO m, und da Entity a noch meist abstrakt ist, sind Sie mit den beiden Einschränkungen links, die PersistRecordBackend definieren. Sie konnten das Synonym als Abkürzung in der Tat verwenden:

insertEnt 
    :: PersistRecordBackend (Entity a) SqlBackend 
    => Entity a -> IO (Key (Entity a)) 
+0

Oh richtig, dass Zwang fehlt auch ein Teil der Unterschrift sein sollte. Ich werde mit einer Erklärung aktualisieren. –

Verwandte Themen