Can't use parameters in create view statement

Issue #216 closed
Semyon Perepelitsa
created an issue

Test case:

gem "minitest", "~> 5.0"
gem "pg", "0.18.1"

require "minitest/autorun"
require "pg"

class PGBugTest < Minitest::Test
  def setup
    @conn = PG.connect
    @param = "hi"
    @result = [{"a" => @param}]

  def test_exec
    @conn.exec("create temp view foo as select #{@conn.escape_literal(@param)}::text a")
    assert_equal @result, @conn.exec("select * from foo").to_a

  def test_prepare
    @conn.prepare("foo", "create temp view foo as select $1::text a")
    @conn.exec_prepared("foo", [@param])
    assert_equal @result, @conn.exec("select * from foo").to_a

  def test_exec_params
    @conn.exec_params("create temp view foo as select $1::text a", [@param])
    assert_equal @result, @conn.exec("select * from foo").to_a

  def test_no_view
    assert_equal @result, @conn.exec_params("select $1::text a from foo", [@param]).to_a
Run options: --seed 23329

# Running:


Finished in 0.014328s, 279.1736 runs/s, 139.5868 assertions/s.

  1) Error:
PG::IndeterminateDatatype: ERROR:  could not determine data type of parameter $1

    pg_error_example.rb:26:in `exec_params'
    pg_error_example.rb:26:in `test_exec_params'

  2) Error:
PG::ProtocolViolation: ERROR:  bind message supplies 1 parameters, but prepared statement "foo" requires 0

    pg_error_example.rb:21:in `exec_prepared'
    pg_error_example.rb:21:in `test_prepare'

4 runs, 2 assertions, 0 failures, 2 errors, 0 skips

As you can see I'm not able to use bind parameters inside "create view" statement but inline parameter works fine. Outside of "create view" bind parameters work fine too. Am I doing it wrong or is it a bug?

Comments (3)

  1. Semyon Perepelitsa reporter

    I guess, that makes sense. You don't want to create a view with one name more than one time. Is the error message correct though? When I try the same in psql I see a more clear syntax error:

    ERROR:  syntax error at or near "create"
    LINE 1: prepare foo as create temp view foo as select $1::text a;
  2. Log in to comment