Ruby `prepare': ERROR: prepared statement "should_insert" already exists (PG::DuplicatePstatement)

Ruby `prepare': ERROR: prepared statement "should_insert" already exists (PG::DuplicatePstatement)

我看过 link

How to fix PG::DuplicatePstatement: ERROR?

但它仍然没有解决我收到的错误消息

`prepare': ERROR:  prepared statement "should_insert" already exists (PG::DuplicatePstatement)

我应该把上述答案中的代码块放在哪里 link?我必须调用方法才能执行吗?

db_connection = PGconn.connect("localhost", 5433, '', '', "dev_ddb", "user", "pass")

db_connection.prepare('should_insert', 'SELECT COUNT(*) from users where user_id = ')

我能够通过取消分配准备好的语句来使它工作。我在 exec_prepared 语句之后插入了这一行。

db_connection.exec("DEALLOCATE should_insert")

您总是可以尝试使用 begin-rescue 块来检查语句是否已经准备好,例如:

begin 
  db_connection.describe_prepared("should_insert")
rescue PG::InvalidSqlStatementName
  db_connection.prepare("should_insert",<<-SQL)
    'SELECT COUNT(*) from users where user_id = '
    SQL
end

然后用

执行
db_connection.exec_prepared