Comments (13)
The type variable is ambiguous. The compiler wants it due to the monomorphism restriction. You need to either explicitly type the query with a concrete SELECT
syntax to use or declare the query polymorphic.
If you want the compiler to infer the type, you have to use the query with a particular backend somewher in the program.
Here's one way to explicitly type it polymorphically, using type holes:
query :: forall select s. Q select _ _ s _
You'll likely get constraints missing on the select
type variable. This is expected. In order to run the query, the backend SELECT
syntax will need to meet certain requirements, and these constraints will make sure of that.
Or monomorphically, you can do
import Database.Beam.Postgres
query :: Q PgSelectSyntax _ _ s _
Does that make sense?
from beam.
Hm.. Theoretically it perfectly does. However, for me just trying to get my "hands dirty" and dump some sql, its a bit overwhelming. As I understand, to be able to treat the query as something "materialized" it has to belong to some backend.
With the purpose of just dumping the sql, how do I declare such query?
from beam.
The generated query is going to depend on the backend in use. Moreover, for certain backends -- such as postgres -- the query generated depends on the connection itself, due to connection-specific string escaping behavior. Thus, there's no canonical representation of a SQL statement.
The tutorial shows several strategies for dumping SQL. Firstly, you can dump the statements beam is about to run using withDatabaseDebug
and passing in an appropriate logging function.
As the manual describes, you can also use an internal debug representation of the query to dump out a SQL-92 like expression. This query should not be used to execute the statement, due to backend-specific-escaping concerns.
To quote
dumpSqlSelect (all_ (persons exampleDb))
The backend syntax in use here is SqlSyntaxBuilder
which is an idealized version of a mostly SQL92-compliant backend.
from beam.
After changing to
{-# LANGUAGE TypeOperators, OverloadedStrings, PartialTypeSignatures #-}
module Main where
import Schema
import Database.Beam
import Database.Beam.Postgres
import Lib
main :: IO ()
main = dumpSqlSelect (all_ (_table2 db))
there's still plenty of type failures..
from beam.
Can you perhaps check the attached archive with sample project? Its stack-based, and supposed to be unpacked inside the top beam directory, near the "beam-core", "beam-migrate" folders (which are on the latest of next-gen-beam-migrate branch). I must be missing something obvious probably.
from beam.
The first problem is:
Found hole:
_table2
:: DatabaseSettings be0 Schema.Db
-> DatabaseEntity be db0 (TableEntity table0)
The Database
constructor isn't exported from Schema.hs
. This is a bug in the code generation. You can go ahead and modify the file directly, and I will upload a patch later today.
So, to be clear, change
module Schema (db, migration) where
to
module Schema (Db(..), db, migration) where
The second problem is that the beam-migrate
utility generates a migratable database, which is a bit more complicated than what SqlSyntaxBuilder
allows. I'll work on this today, and upload a patch this evening. Sorry about that.
In the meantime, you can try using the withDatabaseDebug
technique to execute against a database.
from beam.
Thanks for the help! I'll continue tomorrow.
from beam.
Try now... I think I got it working on my laptop
$ stack exec -- simple-exe
SELECT `t0`.`id` AS "res0", `t0`.`fkColumn` AS "res1" FROM "table2" AS "t0"
from beam.
Hm.. Same branch? Doesn't work for me:
[2 of 2] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/simple-exe/simple-exe-tmp/Main.o )
/home/nickolay/workspace/Haskell/simple/app/Main.hs:14:37: error:
• Couldn't match type ‘Database.Beam.Migrate.SQL.SQL92.Sql92AlterTableColumnSchemaSyntax
(Database.Beam.Migrate.SQL.SQL92.Sql92AlterTableAlterTableActionSyntax
(Database.Beam.Migrate.SQL.SQL92.Sql92DdlCommandAlterTableSyntax
syntax0))’
with ‘Database.Beam.Migrate.SQL.SQL92.Sql92CreateTableColumnSchemaSyntax
(Database.Beam.Migrate.SQL.SQL92.Sql92DdlCommandCreateTableSyntax
syntax0)’
arising from a use of ‘db’
The type variable ‘syntax0’ is ambiguous
• In the first argument of ‘_table2’, namely ‘db’
In the first argument of ‘all_’, namely ‘(_table2 db)’
In the first argument of ‘dumpSqlSelect’, namely
‘(all_ (_table2 db))’
from beam.
You pulled the latest beam-core
? Sometimes you have to do stack clean beam-core
to have it rebuild
from beam.
Yes, works after "stack clean"! Yay :) Thanks for the help, I'll be figuring the next step..
from beam.
@samuraijack Wonderful! Can I close this issue, then?
from beam.
Yes, sure.
from beam.
Related Issues (20)
- A variant of select that doesn't require QBaseScope HOT 2
- Faster pull request workflow. HOT 5
- Unnecessary subquery layers in unionAll_ for Postgres HOT 1
- LensFor: `Couldn't match expected type p` HOT 7
- Requesting `subselectWith_` combinator HOT 1
- beam-postgres: runBeamPostgres is so hard to find in doc
- Lens example in tutorial does not work HOT 1
- GHC 9.6 failures HOT 3
- beam-sqlite: No `instance HasSqlEqualityCheck Sqlite Day` HOT 1
- WHERE clauses are silently dropped if there is no FROM HOT 1
- beam-sqlite: support for `LIKE` + `ESCAPE` ? HOT 1
- free-5.2 not allowed
- vector-0.13 not allowed
- primitive-0.8 not allowed HOT 3
- How to get primary key type? HOT 1
- Subquery without aggregation
- Multicolumn aggregate HOT 1
- Is there any way to reuse a `ToField`, `FromField` definitions from sqlite-simple? HOT 1
- Precise type for `max_` aggregate function
- Postgres column doesn't roundtrip for type `Maybe (Vector Text)`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from beam.