Sequel: Cannot save model or limit varchars

Hi out there,

I experienced Problems when I tried to save a Sequel::Model

   ERROR database: ERROR: currval of sequence "schedules_id_seq" is not yet defined in this session

The model schema is:

   set_schema do
     serial :id, :primary_key => true
     varchar :state, :default => 'running'

     timestamp :starts_at
     timestamp :stops_at

     integer :aberrate_with
     integer :dispersal_shift
   end

During testing I always recreate the models table.

Also I cant figure out how to limit varchars...

Sincerely
Florian

Huh, did some hacking... to fix this problem I changed the sequel/postgres.rb code a bit:

@L167:

   # See: http://www.postgresql.org/docs/8.2/static/sql-insert.html
   RETURNING = " RETURNING (%s)".freeze

   # Initializes and returns @table_sequence
   def table_sequences
     # define default procedure for missing keys (tables) that fetches the pkey
     @table_sequences ||= Hash.new do |sequences, table|
       # this is not threadsafe but the results shouldn't differ
       @pool.hold { |conn| sequences[table] = conn.pkey_and_sequence(table).first }
       sequences[table]
     end
   end
   # Ahh, protect this Method... *paranoia*
   protected :table_sequences

   def execute_insert(sql, table)
     # add RETURNING statement to sql
     sql << RETURNING % table_sequences[table]

     @logger.info(sql) if @logger

     # this is not nice :frowning:
     @pool.hold do |conn|
       result = conn.execute sql
       id = result[0][0].to_i and result.clear

       id
     end

   rescue => e
     @logger.error(e.message) if @logger
     raise e

   end

@L1: I'd replace:

   if !Object.const_defined?('Sequel')
     require File.join(File.dirname(__FILE__), '../sequel')
   end

with:

   require File.join(File.dirname(__FILE__), '../sequel')

since Ruby itself checks whether a file is already required...

Another question:
Is PGconn the right place for the @table_sequences cache? I'd place it in the Sequel::Postgres module...

Sincerely
Florian

···

Am 05.06.2007 um 00:09 schrieb Florian Aßmann:

Hi out there,

I experienced Problems when I tried to save a Sequel::Model

  ERROR database: ERROR: currval of sequence "schedules_id_seq" is not yet defined in this session

The model schema is:

  set_schema do
    serial :id, :primary_key => true
    varchar :state, :default => 'running'

    timestamp :starts_at
    timestamp :stops_at

    integer :aberrate_with
    integer :dispersal_shift
  end

During testing I always recreate the models table.

Also I cant figure out how to limit varchars...

Sincerely
Florian

Not that I mind, but you mind get a better response on the Sequel
mailing list: http://groups.google.com/group/sequel-talk

···

On Jun 4, 7:01 pm, Florian Aßmann <florian.assm...@email.de> wrote:

Huh, did some hacking... to fix this problem I changed the sequel/
postgres.rb code a bit:

@L167:

   # See:http://www.postgresql.org/docs/8.2/static/sql-insert.html
   RETURNING = " RETURNING (%s)".freeze

   # Initializes and returns @table_sequence
   def table_sequences
     # define default procedure for missing keys (tables) that
fetches the pkey
     @table_sequences ||= Hash.new do |sequences, table|
       # this is not threadsafe but the results shouldn't differ
       @pool.hold { |conn| sequences[table] = conn.pkey_and_sequence
(table).first }
       sequences[table]
     end
   end
   # Ahh, protect this Method... *paranoia*
   protected :table_sequences

   def execute_insert(sql, table)
     # add RETURNING statement to sql
     sql << RETURNING % table_sequences[table]

     @logger.info(sql) if @logger

     # this is not nice :frowning:
     @pool.hold do |conn|
       result = conn.execute sql
       id = result[0][0].to_i and result.clear

       id
     end

   rescue => e
     @logger.error(e.message) if @logger
     raise e

   end

@L1: I'd replace:

   if !Object.const_defined?('Sequel')
     require File.join(File.dirname(__FILE__), '../sequel')
   end

with:

   require File.join(File.dirname(__FILE__), '../sequel')

since Ruby itself checks whether a file is already required...

Another question:
Is PGconn the right place for the @table_sequences cache? I'd place
it in the Sequel::Postgres module...

Sincerely
Florian

Am 05.06.2007 um 00:09 schrieb Florian Aßmann:

> Hi out there,

> I experienced Problems when I tried to save a Sequel::Model

> ERROR database: ERROR: currval of sequence "schedules_id_seq" is
> not yet defined in this session

> The model schema is:

> set_schema do
> serial :id, :primary_key => true
> varchar :state, :default => 'running'

> timestamp :starts_at
> timestamp :stops_at

> integer :aberrate_with
> integer :dispersal_shift
> end

> During testing I always recreate the models table.

> Also I cant figure out how to limit varchars...

> Sincerely
> Florian