morphismtech / squeal

Squeal, a deep embedding of SQL in Haskell
362 stars 32 forks source link

Did you encounter a confusing type error? Please comment here. #24

Open echatav opened 6 years ago

echatav commented 6 years ago

I'd love to improve the value of type error messages using custom TypeErrors but to do that I need to collect some examples. If you have an example please share it here with a snippet of code that caused it.

mwotton commented 5 years ago
error:
    • Couldn't match type ‘RowOf
                             (records-sop-0.1.0.2:Generics.SOP.Record.ToRecordCode_Datatype
                                StockId
                                (Generics.SOP.Universe.DatatypeInfoOf StockId)
                                (Generics.SOP.Universe.Code StockId))’
                     with ‘'["unStockId" ::: 'NotNull 'PGint8]’
      Expected type: Manipulation_
                       Schemas
                       (Owner, ResourceTypeId, Scope, ResourcePoolId, Int32, UTCTime,
                        Maybe UTCTime)
                       StockId
        Actual type: Manipulation
                       '[]
                       '["public"
                         ::: '["resource"
                               ::: 'Table Range.Resource.Database.Schema.V1.ResourceTable,
                               "owner" ::: 'Table Range.Resource.Database.Schema.V1.OwnerTable,
                               "scope" ::: 'Table Range.Resource.Database.Schema.V1.ScopeTable,
                               "resource_pool"
                               ::: 'Table Range.Resource.Database.Schema.V1.ResourcePoolTable,
                               "stock" ::: 'Table Range.Resource.Database.Schema.V1.StockTable,
                               "allocation"
                               ::: 'Table Range.Resource.Database.Schema.V1.AllocationTable]]
                       '[ 'NotNull 'PGtext, 'NotNull 'PGtext, 'NotNull 'PGtext,
                          'NotNull 'PGtext, 'NotNull 'PGint4, 'NotNull 'PGtimestamptz,
                          'Null 'PGtimestamptz]
                       '["unStockId" ::: 'NotNull 'PGint8]
    • In the expression:
        insertInto
          #stock
          (Values_
             (Default `as` #id
                :*
                  Set (param @1) `as` #owner_id
                    :*
                      Set (param @2) `as` #resource_id
                        :*
                          Set (param @3) `as` #scope_id
                            :*
                              Set (param @4) `as` #resource_pool_id
                                :*
                                  Set (param @5) `as` #quantity
                                    :*
                                      Set (param @6) `as` #start_time
                                        :* Set (param @7) `as` #end_time))
          OnConflictDoRaise
          (Returning_ (#id `as` #unStockId))
      In an equation for ‘insertStockQ’:
          insertStockQ
            = insertInto
                #stock
                (Values_
                   (Default `as` #id
                      :*
                        Set (param @1) `as` #owner_id
                          :*
                            Set (param @2) `as` #resource_id
                              :*
                                Set (param @3) `as` #scope_id
                                  :*
                                    Set (param @4) `as` #resource_pool_id
                                      :*
                                        Set (param @5) `as` #quantity
                                          :*
                                            Set (param @6) `as` #start_time
                                              :* Set (param @7) `as` #end_time))
                OnConflictDoRaise
                (Returning_ (#id `as` #unStockId))
   |
58 | insertStockQ = insertInto #stock
   |                ^^^^^^^^^^^^^^^^^...

in this case, I had forgotten to declare

instance SOP.Generic StockId
instance SOP.HasDatatypeInfo StockId

but the correspondence between Manipulation and Manipulation_ makes it hard to see what the actual problem is.

ismaelbouyaf commented 5 years ago

Edit: TypeApplication solved the thing, I was missing the FromValue x y => from the fromValue signature

I’m trying to create an instance FromValue 'PGjsonb A where (https://hackage.haskell.org/package/squeal-postgresql-0.4.0.0/docs/Squeal-PostgreSQL-Binary.html#g:2) I saw that there were a FromJSON x => FromValue PGjsonb (Jsonb x) already defined, and since my A is already a FromJSON I thought I could simply do fromValue = fromValue >>= getJsonb but the compiler doesn’t like the internal fromValue:

/home/ismael/workdir/admin-user/src/AdminUser/Model/Rights.hs:38:15: error:
    • Ambiguous type variable ‘pg0’ arising from a use of ‘fromValue’
      prevents the constraint ‘(FromValue
                                  pg0
                                  (Jsonb
                                     (binary-parser-0.5.5:BinaryParser.BinaryParser
                                        A)))’ from being solved.
      Probable fix: use a type annotation to specify what ‘pg0’ should be.
      These potential instances exist:
        instance Squeal.PostgreSQL.Binary.FromArray
                   ('NotNull ('PGfixarray n ty)) y =>
                 FromValue ('PGfixarray n ty) y
          -- Defined in ‘Squeal.PostgreSQL.Binary’
        instance A.FromJSON x => FromValue 'PGjsonb (Jsonb x)
          -- Defined in ‘Squeal.PostgreSQL.Binary’
        instance Squeal.PostgreSQL.Binary.FromArray
                   ('NotNull ('PGvararray ty)) y =>
                 FromValue ('PGvararray ty) y
          -- Defined in ‘Squeal.PostgreSQL.Binary’
    • In the first argument of ‘(>>=)’, namely ‘fromValue’
      In the expression: fromValue >>= getJsonb
      In an equation for ‘fromValue’: fromValue = fromValue >>= getJsonb
   |
38 |   fromValue = fromValue >>= getJsonb
   |               ^^^^^^^^^

What I want is in the propositions instance A.FromJSON x => FromValue 'PGjsonb (Jsonb x) but I don’t find a way to "annotate" fromValue to specify it, since the pg0 will not be in the annotation anyway

Did I miss something to make the compiler make the right choice?

mwotton commented 5 years ago

The error for not qualifying a reference to a column with its table name is a bit confusing, especially when you're moving from selecting from a single table to an innerJoin with another one and suddenly you get errors for all the columns you were selecting in an unqualified way.

ilyakooo0 commented 5 years ago

It was a very simple problem: I called manipulateParams, passing a tuple with not enough values.

On Sat, 2 Nov 2019 at 09:27, Eitan Chatav notifications@github.com wrote:

@mwotton https://github.com/mwotton can you post a minimal example?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/morphismtech/squeal/issues/24?email_source=notifications&email_token=ABPMAWYZBDNJB553XK3XWJDQRUMTRA5CNFSM4EXXYIUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC4VAVY#issuecomment-549015639, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPMAW7FHJC3V6FCFCWN3S3QRUMTRANCNFSM4EXXYIUA .