Skip to content

Loading…

Nils in pg_row pg_arrays #548

Closed
jamiehodge opened this Issue · 6 comments

2 participants

@jamiehodge

Hi Jeremy,

class Description < Sequel::Model(:description)
plugin :pg_row
end

class Collection < Sequel::Model
end

Collection.create description: KU::Media::Description.new(title: 'for', authors: [], keywords: [], tags: [], links: []) # works

Collection.create description: KU::Media::Description.new(title: 'for') # fails

TypeError: can't convert nil into String

method parse_pg_array in sequel_pg.rb at line 93
method call in sequel_pg.rb at line 93
method block in convert_columns in pg_row.rb at line 324
method map in pg_row.rb at line 324
method convert_columns in pg_row.rb at line 324
method call in pg_row.rb at line 286
method yield_hash_rows in postgres.rb at line 547
method block in fetch_rows in postgres.rb at line 547
method execute in postgres.rb at line 134
method _execute in postgres.rb at line 431
method block (2 levels) in execute in postgres.rb at line 234
method check_database_errors in postgres.rb at line 443
method block in execute in postgres.rb at line 234
method block in synchronize in connecting.rb at line 229
method hold in threaded.rb at line 92
method synchronize in connecting.rb at line 229
method execute in postgres.rb at line 234
method execute in actions.rb at line 762
method fetch_rows in postgres.rb at line 547
method returning_fetch_rows in actions.rb at line 821
method insert in actions.rb at line 315
method insert in postgres.rb at line 988
method insert_select in postgres.rb at line 1003
method _insert_select_raw in base.rb at line 1528
method _insert in base.rb at line 1501
method block (2 levels) in _save in base.rb at line 1560
method around_create in base.rb at line 846
method block in _save in base.rb at line 1557
method around_save in base.rb at line 846
method _save in base.rb at line 1552
method block (2 levels) in save in base.rb at line 1233
method block in checked_transaction in base.rb at line 1701
method _transaction in query.rb at line 300
method block in transaction in query.rb at line 263
method block in synchronize in connecting.rb at line 229
method hold in threaded.rb at line 105
method synchronize in connecting.rb at line 229
method transaction in query.rb at line 261
method checked_transaction in base.rb at line 1701
method block in save in base.rb at line 1233
method checked_save_failure in base.rb at line 1689
method save in base.rb at line 1233
method create in base.rb at line 163
method

in test.rb at line 6

@jeremyevans
Owner

This appears to be a definite bug, I'll look into it right away.

@jeremyevans jeremyevans added a commit that closed this issue
@jeremyevans Handle nil values when parsing composite types in the pg_row extensio…
…n (Fixes #548)

Conversion procs are not expected to handle nil values, so they
should not be called if the value is nil.
ed3b8a5
@jamiehodge

Thank you

@jamiehodge

Hi Jeremy,

It looks like there is still a lingering issue:

This works:

Collection.create description: Description.load(title: 'foo', summary: 'bar', keywords: [], links: [], tags: [], authors: [])

This fails:

Collection.create description: Description.load(title: 'foo')

Sequel::DatabaseError: PG::Error: ERROR: array value must start with "{" or dimension information

method exec_prepared    in postgres.rb at line 437 
method _execute_prepared_statement  in postgres.rb at line 437
method block (2 levels) in execute_prepared_statement   in postgres.rb at line 493
method log_yield    in logging.rb at line 33
method block in execute_prepared_statement  in postgres.rb at line 493
method check_disconnect_errors  in postgres.rb at line 111
method execute_prepared_statement   in postgres.rb at line 493
method _execute in postgres.rb at line 429
method block (2 levels) in execute  in postgres.rb at line 234
method check_database_errors    in postgres.rb at line 443
method block in execute in postgres.rb at line 234
method block in synchronize in connecting.rb at line 229
method hold in threaded.rb at line 92
method synchronize  in connecting.rb at line 229
method execute  in postgres.rb at line 234
method execute  in actions.rb at line 762
method execute  in postgres.rb at line 619
method execute  in postgres.rb at line 644
method fetch_rows   in postgres.rb at line 547
method each in actions.rb at line 151
method each in sequel_pg.rb at line 58
method single_record    in actions.rb at line 533
method first    in actions.rb at line 206
method run  in prepared_statements.rb at line 137
method run  in postgres.rb at line 931
method call in prepared_statements.rb at line 26
method call in postgres.rb at line 636
method _insert_select_raw   in prepared_statements.rb at line 151
method _insert  in base.rb at line 1501
method block (2 levels) in _save    in base.rb at line 1560
method around_create    in base.rb at line 846
method block in _save   in base.rb at line 1557
method around_save  in base.rb at line 846
method _save    in base.rb at line 1552
method block (2 levels) in save in base.rb at line 1233
method block in checked_transaction in base.rb at line 1701
method _transaction in query.rb at line 321
method block in transaction in query.rb at line 283
method block in synchronize in connecting.rb at line 229
method hold in threaded.rb at line 105
method synchronize  in connecting.rb at line 229
method transaction  in query.rb at line 276
method checked_transaction  in base.rb at line 1701
method block in save    in base.rb at line 1233
method checked_save_failure in base.rb at line 1689
method save in base.rb at line 1233
method create   in base.rb at line 163
method <main>   in test.rb at line 3
@jeremyevans
Owner

I'll take a look at this tomorrow. In the future, can you please submit an SQL log and if possible a self contained example showing the issue? The SQL log especially would help in this case.

This appears to be related to prepared statements, so until this issue is fixed, you could just not use the prepared_statements plugin.

@jeremyevans jeremyevans reopened this
@jeremyevans
Owner

I gave a brief shot and couldn't recreate the error message you are getting, but can you try this patch and let me know if it fixes the issue?

diff --git a/lib/sequel/extensions/pg_array.rb b/lib/sequel/extensions/pg_array.rb
index 0f523b0..abb3621 100644
--- a/lib/sequel/extensions/pg_array.rb
+++ b/lib/sequel/extensions/pg_array.rb
@@ -231,6 +231,8 @@ module Sequel
         # Format arrays used in bound variables.
         def bound_variable_array(a)
           case a
+          when nil
+            ""
           when Array
             "{#{a.map{|i| bound_variable_array(i)}.join(COMMA)}}"
           when Sequel::SQL::Blob
@jeremyevans
Owner

That last diff was bad, since apparently NULL formatting in bound variables is different for arrays than it is for row types (so the diff would fix pg_row, but break pg_array). I'm currently testing a better fix, hopefully it should be pushed shortly.

@jeremyevans jeremyevans added a commit that closed this issue
@jeremyevans Handle nil values when formatting bound variable arguments in the pg_…
…row extension (Fixes #548)

Basically, pg_row piggy-backs on pg_array bound variable
formatting, but array types use NULL to represent NULL,
while row types use the empty string to represent NULL.

While here, add tests for nil values in bound variables for
arrays and hstore, even though the behavior there was already
correct.
a8b135d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.