On Wednesday, April 16, 2014 9:24:05 PM UTC-7, Andrew Hacking wrote:
>
> Wondering how I can specify a computed model attribute which is always 
> returned for my model.
>
> I would like to have an automatically computed sequence scoped on a column 
> as follows:
>
> DB[:table].select_append{row_number(:over, partition: owner_id, order: 
>> created_at){}.as(:item_number)}.from_self
>
>
> This returns the row_number as the item_number for all items scoped to 
> owner_id, such that those numbers are monotonically increasing. It works 
> perfectly.
>
> I have tried to add the following in my model class:
>
> set_dataset DB[:table].select_append{row_number(:over, partition: 
>> owner_id, order: created_at){}.as(:item_number)}.from_self
>
>
> But I get the following error:
>
> sequel-4.6.0/lib/sequel/dataset/sql.rb:640:in `schema_and_table': 
>> table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, 
>> or String (Sequel::Error)
>
> sequel-4.6.0/lib/sequel/database/schema_methods.rb:790:in 
>> `schema_and_table'
>> sequel-4.6.0/lib/sequel/adapters/shared/postgres.rb:944:in `regclass_oid'
>> sequel-4.6.0/lib/sequel/adapters/shared/postgres.rb:340:in `indexes'
>> sequel-4.6.0/lib/sequel/plugins/auto_validations.rb:111:in 
>> `setup_auto_validations'
>> sequel-4.6.0/lib/sequel/model/plugins.rb:44:in `block in 
>> after_set_dataset'
>
>
> Any ideas how I can have my models include a (read only) attribute 
> computed from a window function? 
>

Well, from the backtrace, a quick fix would be to not use the 
auto_validations plugin.

The patch below should fix things (assuming Google Groups doesn't mangle 
it).  I'll have to add a spec before I can commit it, though.

Thanks,
Jeremy

diff --git a/lib/sequel/plugins/auto_validations.rb 
b/lib/sequel/plugins/auto_validations.rb
index e51b0f8..d40dc9d 100644
--- a/lib/sequel/plugins/auto_validations.rb
+++ b/lib/sequel/plugins/auto_validations.rb
@@ -107,8 +107,9 @@ module Sequel
           @auto_validate_not_null_columns = not_null_cols - 
Array(primary_key)
           explicit_not_null_cols += Array(primary_key)
           @auto_validate_explicit_not_null_columns = 
explicit_not_null_cols.uniq
-          @auto_validate_unique_columns = if db.supports_index_parsing?
-            db.indexes(dataset.first_source_table).select{|name, idx| 
idx[:unique] == true}.map{|name, idx| idx[:columns]}
+          table = dataset.first_source_table
+          @auto_validate_unique_columns = if db.supports_index_parsing? && 
[Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| 
table.is_a?(c)}
+            db.indexes(table).select{|name, idx| idx[:unique] == 
true}.map{|name, idx| idx[:columns]}
           else
             []
           end

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to