ddario
ddario

Reputation: 1035

Sequel default example fails when switched to postgres adapter

I'm trying to run the Sequel example from http://sequel.rubyforge.org/. Everything works fine on sqlite, but fails when I switch to postgres.

This is the connection code:

DB = Sequel.connect(:adapter=>'postgres', :host=>'localhost', :database=>'testing', :user=>'postgres', :default_schema=>'sequel')

This is the error I get:

postgres.rb:145:in `async_exec': PG::Error: ERROR:  relation "items" does not exist (Sequel::DatabaseError) 
LINE 1: INSERT INTO "items" ("price", "name") VALUES (12.45377636338...

I'm suspecting that the issue is Sequel trying to execute INSERT INTO "items" instead "sequel.items", even though :default_schema is correctly set.

Anyone have any idea what i'm doing wrong? Thanks in advance.

Edit - this is the code used:

require "rubygems"
require "sequel"

# connect to an in-memory database
#DB = Sequel.sqlite
DB = Sequel.connect(:adapter=>'postgres', :host=>'localhost', :database=>'testing', :user=>'postgres', :default_schema=>'sequel')

# create an items table
DB.create_table :items do
  primary_key :id
  String :name
  Float :price
end

# create a dataset from the items table
items = DB[:items]

# populate the table
items.insert(:name => 'abc', :price => rand * 100)
items.insert(:name => 'def', :price => rand * 100)
items.insert(:name => 'ghi', :price => rand * 100)

# print out the number of records
puts "Item count: #{items.count}"

Upvotes: 0

Views: 723

Answers (1)

AJcodez
AJcodez

Reputation: 34146

Looks like you're missing the password in the connect method (that's the only difference from the documentation example). Its common for the password to just be the username, so try that if you're not sure what the password is.

It's also suggested to use a different postgresql user with each project, which also makes naming the user intuitive (the project name.) That avoids potentially clashing names.

Anyway, see if this works:

DB = Sequel.postgres 'testing', host: 'localhost', default_schema: 'sequel', 
       user: 'postgres', password: 'postgres'

Upvotes: 1

Related Questions