# Sequel: Cannot save model or limit varchars

**URL:** <https://www.ruby-forum.com/t/sequel-cannot-save-model-or-limit-varchars/105963>\
**Category:** Ruby\
**Created:** [June 5, 2007, 12:10am UTC](https://www.ruby-forum.com/t/sequel-cannot-save-model-or-limit-varchars/105963 "2007-06-05T00:10:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_ASSSSmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@Florian\_ASSSSmann](https://www.ruby-forum.com/u/Florian_ASSSSmann)\
**Post date:** [June 5, 2007, 12:10am UTC](https://www.ruby-forum.com/t/sequel-cannot-save-model-or-limit-varchars/105963/1 "2007-06-05T00:10:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Florian\_ASSSSmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@Florian\_ASSSSmann](https://www.ruby-forum.com/u/Florian_ASSSSmann)\
**Post date:** [June 5, 2007, 2:01am UTC](https://www.ruby-forum.com/t/sequel-cannot-save-model-or-limit-varchars/105963/2 "2007-06-05T02:01:21Z")

</div>

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

@L167:

# See: [PostgreSQL: Documentation: 8.2: INSERT](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 :(
 @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:

---

<div class="post-metadata">

**Author:** ![Florian\_ASSSSmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@Florian\_ASSSSmann](https://www.ruby-forum.com/u/Florian_ASSSSmann)\
**Post date:** [June 5, 2007, 4:32am UTC](https://www.ruby-forum.com/t/sequel-cannot-save-model-or-limit-varchars/105963/3 "2007-06-05T04:32:37Z")

</div>

On Jun 4, 7:01 pm, Florian Aßmann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> # define default procedure for missing keys (tables) that  
> 
> ```
> 
> Another question:
> 
> > I experienced Problems when I tried to save a Sequel::Model  
> > timestamp :starts\_at  
> > 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](http://groups.google.com/group/sequel-talk)
