Coder Social home page Coder Social logo

Can't create "all_" query about beam HOT 13 CLOSED

haskell-beam avatar haskell-beam commented on August 16, 2024
Can't create "all_" query

from beam.

Comments (13)

tathougies avatar tathougies commented on August 16, 2024

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.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

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.

tathougies avatar tathougies commented on August 16, 2024

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.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

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.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

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.

tathougies avatar tathougies commented on August 16, 2024

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.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

Thanks for the help! I'll continue tomorrow.

from beam.

tathougies avatar tathougies commented on August 16, 2024

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.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

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.

tathougies avatar tathougies commented on August 16, 2024

You pulled the latest beam-core? Sometimes you have to do stack clean beam-core to have it rebuild

from beam.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

Yes, works after "stack clean"! Yay :) Thanks for the help, I'll be figuring the next step..

from beam.

tathougies avatar tathougies commented on August 16, 2024

@samuraijack Wonderful! Can I close this issue, then?

from beam.

canonic-epicure avatar canonic-epicure commented on August 16, 2024

Yes, sure.

from beam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.