On Sat, Nov 4, 2023 at 7:28 PM Jeremy Evans <[email protected]> wrote:

> On Sat, Nov 4, 2023 at 12:55 PM Juan Manuel Cuello <[email protected]>
> wrote:
>
>> Hi,
>>
>> I'm having problems when using the pg_json extension with models that
>> load the table name using Seguel.delay.
>>
>> DB.extension :pg_json
>>
>> This works as expected:
>>
>> class Foo < Sequel::Model(DB.from(:foo))
>> end
>>
>> Foo.new(json_column: { a: 1 }).json_column.class
>> => Sequel::Postgres::JSONBHash
>>
>> But this doesn't:
>>
>> class Foo < Sequel::Model(DB.from(Sequel.delay{:foo}))
>> end
>>
>
> If you use Sequel.delay for a model's dataset, then Sequel cannot
> determine the schema.  Database#schema only supports datasets selecting
> from a symbol, identifier, qualified identifier, or string.  It could
> theoretically support delayed evaluations, but that's a bad idea in the
> general case, because there is no guarantee that the value at runtime will
> be the same as the value at call time (after all, the whole point of the
> delayed evaluation is to allow for runtime changes).
>
> You can try this in your application:
>
> class Sequel::Model
>   def self.get_db_schema_array(reload)
>      from = dataset.opts[:from][0]
>      if from.is_a?(Sequel::SQL::DelayedEvaluation)
>        dataset  = self.dataset.from(from.call(dataset))
>      end
>      check_non_connection_error(false){db.schema(dataset, :reload=>reload)}
>    end
> end
>

Thanks Jeremy, that solved my problem!

Now I'm wondering what else I've been missing for not getting the schema
with the default implementation of get_db_schema_array, since so far I've
been using Sequel.delay for the model's dataset without any apparent
problems.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CALXCfb2A9gPiBCojHbEwytUW_DK2diHccFOh-Eg44XGK3Q77jw%40mail.gmail.com.

Reply via email to