will / crystal-pg

a postgres driver for crystal
BSD 3-Clause "New" or "Revised" License
462 stars 77 forks source link

Unhandled exception: cannot insert multiple commands into a prepared statement (PQ::PQError) #217

Closed carcinocron closed 3 years ago

carcinocron commented 3 years ago
      @db.unprepared.exec <<-SQL
        CREATE TABLE public.users (
          id BIGSERIAL PRIMARY KEY NOT NULL,
          active BOOLEAN DEFAULT FALSE,
          name UNIQUE,
          email UNIQUE,
          lastlogin_at TIMESTAMPTZ,
          created_at TIMESTAMPTZ DEFAULT NOW(),
          updated_at TIMESTAMPTZ DEFAULT NOW()
        );
        CREATE INDEX IF NOT EXISTS users_active_index ON users (active);
        ALTER TABLE users ADD CONSTRAINT users_email_length CHECK (char_length(handle) > 5);
        CREATE UNIQUE INDEX users_name_unique_lowercase ON users (lower(name));
      SQL
Unhandled exception: cannot insert multiple commands into a prepared statement (PQ::PQError)
  from lib/pg/src/pq/connection.cr:214:7 in 'handle_error'
  from lib/pg/src/pq/connection.cr:197:7 in 'handle_async_frames'
  from lib/pg/src/pq/connection.cr:162:7 in 'read'
  from lib/pg/src/pq/connection.cr:157:7 in 'read'
  from lib/pg/src/pq/connection.cr:408:31 in 'expect_frame'
  from lib/pg/src/pq/connection.cr:407:5 in 'expect_frame'
  from lib/pg/src/pg/statement.cr:18:5 in 'perform_query'
  from lib/pg/src/pg/statement.cr:35:14 in 'perform_exec'
  from lib/db/src/db/statement.cr:82:14 in 'perform_exec_and_release'
  from lib/db/src/db/statement.cr:68:7 in 'exec:args'
  from lib/db/src/db/pool_statement.cr:19:30 in 'exec:args'
  from lib/db/src/db/query_methods.cr:275:7 in 'exec'
  from db/migrations/2020_11_26_044454_create_users_table.migration.cr:6:7 in 'up'
  from sh/migrate_up:46:7 in 'run'
  from sh/migrate_up:58:5 in '__crystal_main'
  from ../../../../usr/share/crystal/src/crystal/main.cr:105:5 in 'main_user_code'
  from ../../../../usr/share/crystal/src/crystal/main.cr:91:7 in 'main'
  from ../../../../usr/share/crystal/src/crystal/main.cr:114:3 in 'main'
  from __libc_start_main
  from _start
  from ???

looks like unprepared isn't working.

straight-shoota commented 3 years ago

exec is only for single statements. You're combining multiple statements. Instead, you should have separate calls for each statement.

Multi-statement support is currently limited by crystal-db (crystal-lang/crystal-db#113).

carcinocron commented 3 years ago

I discovered that exec_all does what I expected unprepared to do.

straight-shoota commented 3 years ago

Ah right, this shard adds #exec_all as an additional method not available in the generic crystal-db API. Sry, I didn't recall that earlier.