Rails 3 and postgresql

I have a simple project created around a very, very simple database: one main table packs with just three rows
Column | Type | Modifiers

Hi!

It seems like the id variable is nil.

Could you check this?

Best Regards,

Everaldo

Thanks Everaldo:
but I have already checked the id variable value by changing:
@album = Pack.find id
to
@album = Pack.find 1
the result is the same.

I wonder if someone has noticed some bug with the pg gem. I do not have any other explanation so far.

Gato Pardo

What happens if, in the rails console, you type
Pack.find 1

Paste the result here.
Also show us db/schema.rb

Colin

What happens if, in the rails console, you type

Pack.find 1

Paste the result here.

Also show us db/schema.rb

Colin

gplus.to/clanlaw

Thanks Colin:

1.- Results with Pack.find 1

"irb(main):001:0> Pack.find 1
Could not log “sql.active_record” event. NoMethodError: undefined method `name’ for nil:NilClass

PGError: ERROR: zero-length delimited identifier at or near “”""
LINE 1: SELECT “packs”.* FROM “packs” WHERE “packs”."" = $1 LIMIT …
^

: SELECT “packs”.* FROM “packs” WHERE “packs”."" = $1 LIMIT 1
ActiveRecord::StatementInvalid: PGError: ERROR: zero-length delimited identifier at or near “”""

LINE 1: SELECT “packs”.* FROM “packs” WHERE “packs”."" = $1 LIMIT …
^
: SELECT “packs”.* FROM “packs” WHERE “packs”."" = $1 LIMIT 1

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:968:in `prepare'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:968:in `exec_cache'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:552:in `exec_query'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `log'

from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `exec_query'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:1031:in `select'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:470:in `find_by_sql'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation.rb:111:in `to_a'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:370:in `find_first'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:122:in `first'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:328:in `find_one'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:304:in `find_with_ids'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:107:in `find'

from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `__send__'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `find'

"

2.- I do not have a file db/schema.rb.
I am just using my database through config/database.yml.

Gato Pardo

Well you should have, you should have used rails migrations to create
the db, unless this is a legacy database. Create schema.rb by running
rake db:schema:dump
and post it here, though whether this is anything to do with the
problem is unlikely.

Colin

Also post model/pack.rb please.

Colin

Well you should have, you should have used rails migrations to create

the db, unless this is a legacy database. Create schema.rb by running

rake db:schema:dump

and post it here, though whether this is anything to do with the

problem is unlikely.

Also post model/pack.rb please.

Colin

Thanks Colin for your fast answer and your care.

I just created schema.rb. Here I send its content:


ActiveRecord::Schema.define(:version => 20110110162357) do

create_table “packs”, :id => false, :force => true do |t|

t.integer "id",                    :null => false
t.string  "nombre", :limit => 40
t.string  "path",   :limit => 250

end

create_table “sessions”, :force => true do |t|

t.string   "session_id", :null => false
t.text     "data"
t.datetime "created_at"
t.datetime "updated_at"

end

add_index “sessions”, [“session_id”], :name => “index_sessions_on_session_id”

add_index “sessions”, [“updated_at”], :name => “index_sessions_on_updated_at”

end

2.- The content of model/pack.rb

Notice how the spec for id is different between the two tables, I
suppose that is due to the fact that you are providing your own
nextval for id (there is no spec for it in sessions so rails will
assume the default id column). I suspect this may be the cause of the
problem. First I suggest reverting id to a normal id column as you
have for sessions and see if this removes the problem. If it does
then either don't use the nextval feature or you will have to ask
others whether it is possible to do this and if so whether you are
doing it correctly. I strongly advise against doing this, life will
be much simpler if you stick to the rails conventions. Possibly you
can find another way to achieve what you want, maybe by adding another
column for your alternative id.

Colin

Notice how the spec for id is different between the two tables, I

suppose that is due to the fact that you are providing your own

nextval for id (there is no spec for it in sessions so rails will

assume the default id column). I suspect this may be the cause of the

problem. First I suggest reverting id to a normal id column as you

have for sessions and see if this removes the problem. If it does

then either don’t use the nextval feature or you will have to ask

others whether it is possible to do this and if so whether you are

doing it correctly. I strongly advise against doing this, life will

be much simpler if you stick to the rails conventions. Possibly you

can find another way to achieve what you want, maybe by adding another

column for your alternative id.

Colin

Thanks a lot Colin:

I did, literarily as you suggested, and I do not have that ugly error with “Packfind id” any more.

I do have another problem, that comes from changes in rails version, but I’ll Google before starting any new thread.

Thanks, once again.

Gato Pardo