The Database Toolkit for Ruby
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 1 commit ahead, 4646 commits behind jeremyevans:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Sequel: The Database Toolkit for Ruby

Sequel is a lightweight database access toolkit for Ruby.

  • Sequel provides thread safety, connection pooling and a concise DSL for constructing database queries and table schemas.

  • Sequel also includes a lightweight but comprehensive ORM layer for mapping records to Ruby objects and handling associated records.

  • Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, master/slave configurations, and database sharding.

  • Sequel makes it easy to deal with multiple records without having to break your teeth on SQL.

  • Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3.


To check out the source code:

git clone git://


If you have any comments or suggestions please post to the Google group.


sudo gem install sequel

A Short Example

require 'rubygems'
require 'sequel'

DB = Sequel.sqlite # memory database

DB.create_table :items do
  primary_key :id
  String :name
  Float :price

items = DB[:items] # Create a dataset

# 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}"

# Print out the average price
puts "The average price is: #{items.avg(:price)}"

The Sequel Console

Sequel includes an IRB console for quick access to databases. You can use it like this:

sequel sqlite://test.db # test.db in current directory

You get an IRB session with the database object stored in DB.

An Introduction

Sequel is designed to take the hassle away from connecting to databases and manipulating them. Sequel deals with all the boring stuff like maintaining connections, formatting SQL correctly and fetching records so you can concentrate on your application.

Sequel uses the concept of datasets to retrieve data. A Dataset object encapsulates an SQL query and supports chainability, letting you fetch data using a convenient Ruby DSL that is both concise and flexible.

For example, the following one-liner returns the average GDP for countries in the middle east region:

DB[:countries].filter(:region => 'Middle East').avg(:GDP)

Which is equivalent to:

SELECT avg(GDP) FROM countries WHERE region = 'Middle East'

Since datasets retrieve records only when needed, they can be stored and later reused. Records are fetched as hashes (or custom model objects), and are accessed using an Enumerable interface:

middle_east = DB[:countries].filter(:region => 'Middle East')
middle_east.order(:name).each{|r| puts r[:name]}

Sequel also offers convenience methods for extracting data from Datasets, such as an extended map method: #=> ['Egypt', 'Greece', 'Israel', ...]

Or getting results as a transposed hash, with one column as key and another as value:

middle_east.to_hash(:name, :area) #=> {'Israel' => 20000, 'Greece' => 120000, ...}

Getting Started

Connecting to a database

To connect to a database you simply provide Sequel with a URL:

require 'sequel'
DB = Sequel.connect('sqlite://blog.db')

The connection URL can also include such stuff as the user name and password:

DB = Sequel.connect('postgres://cico:12345@localhost:5432/mydb')

You can also specify optional parameters, such as the connection pool size, or loggers for logging SQL queries:

DB = Sequel.connect("postgres://postgres:postgres@localhost/my_db",
  :max_connections => 10, :logger =>'log/db.log'))

You can specify a block to connect, which will disconnect from the database after it completes:

Sequel.connect('postgres://cico:12345@localhost:5432/mydb'){|db| db[:posts].delete}

Arbitrary SQL queries

You can execute arbitrary SQL code using Database#run:"create table t (a text, b text)")"insert into t values ('a', 'b')")

You can also create datasets based on raw SQL:

dataset = DB['select id from items']
dataset.count # will return the number of records in the result set # will return an array containing all values of the id column in the result set

You can also fetch records with raw SQL through the dataset:

DB['select * from items'].each do |row|
  p row

You can use placeholders in your SQL string as well:

name = 'Jim'
DB['select * from items where name = ?', name].each do |row|
  p row

Getting Dataset Instances

Datasets are the primary way records are retrieved and manipulated. They are generally created via the Database#from or Database#[] methods:

posts = DB.from(:posts)
posts = DB[:posts] # same

Datasets will only fetch records when you tell them to. They can be manipulated to filter records, change ordering, join tables, etc..

Retrieving Records

You can retrieve all records by using the all method:

# SELECT * FROM posts

The all method returns an array of hashes, where each hash corresponds to a record.

You can also iterate through records one at a time:

posts.each{|row| p row}

Or perform more advanced stuff:

names_and_dates ={|r| [r[:name], r[:date]]}
old_posts, recent_posts = posts.partition{|r| r[:date] < - 7}

You can also retrieve the first record in a dataset:


Or retrieve a single record with a specific value:

posts[:id => 1]
# SELECT * FROM posts WHERE id = 1 LIMIT 1

If the dataset is ordered, you can also ask for the last record:


Filtering Records

An easy way to filter records is to provide a hash of values to match:

my_posts = posts.filter(:category => 'ruby', :author => 'david')
# WHERE category = 'ruby' AND author = 'david'

You can also specify ranges:

my_posts = posts.filter(:stamp => ( - 14)..( - 7))
# WHERE stamp >= '2010-06-30' AND stamp <= '2010-07-07'

Or arrays of values:

my_posts = posts.filter(:category => ['ruby', 'postgres', 'linux'])
# WHERE category IN ('ruby', 'postgres', 'linux')

Sequel also accepts expressions:

my_posts = posts.filter{|o| o.stamp > << 1}
# WHERE stamp > '2010-06-14'

Some adapters will also let you specify Regexps:

my_posts = posts.filter(:category => /ruby/i)
# WHERE category ~* 'ruby'

You can also use an inverse filter:

my_posts = posts.exclude(:category => /ruby/i)
# WHERE category !~* 'ruby'

You can also specify a custom WHERE clause using a string:

posts.filter('stamp IS NOT NULL')

You can use parameters in your string, as well:

author_name = 'JKR'
posts.filter('(stamp < ?) AND (author != ?)', - 3, author_name)
# WHERE (stamp < '2010-07-11') AND (author != 'JKR')
posts.filter{|o| (o.stamp < - 3) & ~{:author => author_name}} # same as above

Datasets can also be used as subqueries:

DB[:items].filter('price > ?', DB[:items].select{|o| o.avg(:price) + 100})
# WHERE price > (SELECT avg(price) + 100 FROM items)

After filtering you can retrieve the matching records by using any of the retrieval methods:

my_posts.each{|row| p row}

See the doc/dataset_filtering.rdoc file for more details.

Summarizing Records

Counting records is easy:

posts.filter(:category => /ruby/i).count
# SELECT COUNT(*) FROM posts WHERE category ~* 'ruby'

And you can also query maximum/minimum values:

max = DB[:history].max(:value)
# SELECT max(value) FROM history

min = DB[:history].min(:value)
# SELECT min(value) FROM history

Or calculate a sum or average:

sum = DB[:items].sum(:price)
# SELECT sum(price) FROM items
avg = DB[:items].avg(:price)
# SELECT avg(price) FROM items

Ordering Records

Ordering datasets is simple:

# ORDER BY stamp
posts.order(:stamp, :name)
# ORDER BY stamp, name

Chaining order doesn't work the same as filter:

# ORDER BY name

The order_more method chains this way, though:

# ORDER BY stamp, name

You can also specify descending order:


Selecting Columns

Selecting specific columns to be returned is also simple:
# SELECT stamp FROM posts, :name)
# SELECT stamp, name FROM posts

Chaining select works like order, not filter:
# SELECT name FROM posts

As you might expect, there is an order_more equivalent for select:
# SELECT stamp, name FROM posts

Deleting Records

Deleting records from the table is done with delete:

posts.filter('stamp < ?', - 3).delete
# DELETE FROM posts WHERE stamp < '2010-07-11'

Be very careful when deleting, as delete affects all rows in the dataset. Filter first, delete second, unless you want to empty the table:

posts.filter('stamp < ?', - 7).delete
posts.delete.filter('stamp < ?', - 7)

Inserting Records

Inserting records into the table is done with insert:

posts.insert(:category => 'ruby', :author => 'david')
# INSERT INTO posts (category, author) VALUES ('ruby', 'david')

Updating Records

Updating records in the table is done with update:

posts.filter('stamp < ?', - 7).update(:state => 'archived')
# UPDATE posts SET state = 'archived' WHERE stamp < '2010-07-07'

You can reference table columns when choosing what values to set:

posts.filter{|o| o.stamp < - 7}.update(:backup_number => :backup_number + 1)
# UPDATE posts SET backup_number = backup_number + 1 WHERE stamp < '2010-07-07'

As with delete, this affects all rows in the dataset, so filter first, update second, unless you want to update all rows:

posts.filter('stamp < ?', - 7).update(:state => 'archived')
posts.update(:state => 'archived').filter('stamp < ?', - 7)

Joining Tables

Sequel makes it easy to join tables:

order_items = DB[:items].join(:order_items, :item_id => :id).
  filter(:order_items__order_id => 1234)

This is equivalent to the SQL:

SELECT * FROM items INNER JOIN order_items
ON order_items.item_id = 
WHERE order_items.order_id = 1234

You can then do anything you like with the dataset:

order_total = order_items.sum(:price)

Which is equivalent to the SQL:

SELECT sum(price) FROM items INNER JOIN order_items
ON order_items.item_id =
WHERE order_items.order_id = 1234

Graphing Datasets

When retrieving records from joined datasets, you get the results in a single hash, which is subject to clobbering if you have columns with the same name in multiple tables:

DB[:items].join(:order_items, :item_id => :id).first
=> {:id=>, :item_id=>order_items.item_id}

Using graph, you can split the result hashes into subhashes, one per join:

DB[:items].graph(:order_items, :item_id => :id).first
=> {:items=>{:id=>}, :order_items=>{:id=>, :item_id=>order_items.item_id}}

An aside: column references in Sequel

Sequel expects column names to be specified using symbols. In addition, returned hashes always use symbols as their keys. This allows you to freely mix literal values and column references in many cases. For example, the two following lines produce equivalent SQL:

items.filter(:x => 1)
# SELECT * FROM items WHERE (x = 1)
items.filter(1 => :x)
# SELECT * FROM items WHERE (1 = x)"

Ruby strings are generally treated as SQL strings:

items.filter(:x => 'x')
# SELECT * FROM items WHERE (x = 'x')

Qualifying column names

Column references can be qualified by using the double underscore special notation :table__column:

# items.price

Column aliases

You can also alias columns by using the triple undersecore special notation :column___alias or :table__column___alias:

# price AS p
# items.price AS p

Another way to alias columns is to use the #as method:

# price AS p

Sequel Models

A model class wraps a dataset, and an instance of that class wraps a single record in the dataset.

Model classes are defined as regular Ruby classes:

DB = Sequel.connect('sqlite://blog.db')
class Post < Sequel::Model

Just like in DataMapper or ActiveRecord, Sequel model classes assume that the table name is a plural of the class name:

Post.table_name #=> :posts

You can, however, explicitly set the table name or even the dataset used:

class Post < Sequel::Model(:my_posts)
# or:
Post.set_dataset :my_posts

If you use a symbol, it assumes you are referring to the table with the same name. You can also give it a dataset, which will set the defaults for all retrievals for that model:

Post.set_dataset DB[:my_posts].filter(:category => 'ruby')
Post.set_dataset DB[:my_posts].select(:id, :name).order(:date)

Model instances

Model instances are identified by a primary key. In most cases, Sequel can introspec the database to determine the primary key, but if not, it defaults to using :id. The Model.[] method can be used to fetch records by their primary key:

post = Post[123]

The Model#pk method is used to retrieve the record's primary key value: #=> 123

Sequel models allow you to use any column as a primary key, and even composite keys made from multiple columns:

class Post < Sequel::Model
  set_primary_key [:category, :title]

post = Post['ruby', 'hello world'] #=> ['ruby', 'hello world']

You can also define a model class that does not have a primary key, but then you lose the ability to easily update and delete records.

A model instance can also be fetched by specifying a condition:

post = Post[:title => 'hello world']
post = Post.find{|o| o.num_comments < 10}

Iterating over records

A model class lets you iterate over subsets of records by proxying many methods to the underlying dataset. This means that you can use most of the Dataset API to create customized queries that return model instances, e.g.:

Post.filter(:category => 'ruby').each{|post| p post}

You can also manipulate the records in the dataset:

Post.filter{|o| o.num_comments < 7}.delete
Post.filter( => 'ruby')

Accessing record values

A model instances stores its values as a hash:

post.values #=> {:id => 123, :category => 'ruby', :title => 'hello world'}

You can read the record values as object attributes, assuming the attribute names are valid columns in the model's dataset: #=> 123
post.title #=> 'hello world'

If the record's attributes names are not valid columns in the model's dataset (maybe because you used select_more to add a computed value column), you can use Model#[] to access the values:

post[:id] #=> 123
post[:title] #=> 'hello world'

You can also change record values:

post.title = 'hey there'
# or 
post.set(:title=>'hey there')

That will just change the value for the object, it will not persist the changes to the database. To persist the record, call the #save method:

If you want to modify record values and save the changes to the object after doing so, use the #update method:

post.update(:title => 'hey there')

Creating new records

New records can be created by calling Model.create:

post = Post.create(:title => 'hello world')

Another way is to construct a new instance and save it later:

post =
post.title = 'hello world'

You can also supply a block to and Model.create:

post = do |p|
  p.title = 'hello world'

post = Post.create{|p| p.title = 'hello world'}


You can execute custom code when creating, updating, or deleting records by defining hook methods. The before_create and after_create hook methods wrap record creation. The before_update and after_update hook methods wrap record updating. The before_save and after_save hook methods wrap record creation and updating. The before_destroy and after_destroy hook methods wrap destruction. The before_validation and after_validation hook methods wrap validation. Example:

class Post < Sequel::Model
  def after_create

  def after_destroy

Note the use of super if you define your own hook methods. Almost all Sequel::Model class and instance methods (not just hook methods) can be overridden safely, but you have to make sure to call super when doing so, otherwise you risk breaking things.

For the example above, you should probably use a database trigger if you can. Hooks can be used for data integrity, but they will only enforce that integrity when you are using the model. If you plan on allowing any other access to the database, it's best to use database triggers for data integrity.

Deleting records

You can delete individual records by calling #delete or #destroy. The only difference between the two methods is that #destroy invokes before_destroy and after_destroy hook methods, while #delete does not:

post.delete #=> bypasses hooks
post.destroy #=> runs hooks

Records can also be deleted en-masse by invoking Model.delete and Model.destroy. As stated above, you can specify filters for the deleted records:

Post.filter(:category => 32).delete #=> bypasses hooks
Post.filter(:category => 32).destroy #=> runs hooks

Please note that if Model.destroy is called, each record is deleted separately, but Model.delete deletes all matching records with a single SQL query.


Associations are used in order to specify relationships between model classes that reflect relationships between tables in the database, which are usually specified using foreign keys.

class Post < Sequel::Model
  many_to_one :author
  one_to_many :comments
  many_to_many :tags

many_to_one creates a getter and setter for each model object:

class Post < Sequel::Model
  many_to_one :author

post = Post.create(:name => 'hi!') = Author[:name => 'Sharon']

one_to_many and many_to_many create a getter method, a method for adding an object to the association, a method for removing an object from the association, and a method for removing all associated objects from the association:

class Post < Sequel::Model
  one_to_many :comments
  many_to_many :tags

post = Post.create(:name => 'hi!')
comment = Comment.create(:text=>'hi')
tag = Tag.create(:tag=>'interesting')

Note that the remove_* and remove_all_* methods do not delete the object from the database, they merely disassociate the associated object from the receiver.

All associations add a dataset method that can be used to further filter or reorder the returned objects, or modify all of them:

# Delete all of this post's comments from the database

# Return all tags related to this post with no subscribers, ordered by the tag's name

Eager Loading

Associations can be eagerly loaded via .eager and the :eager association option. Eager loading is used when loading a group of objects. It loads all associated objects for all of the current objects in one query, instead of using a separate query to get the associated objects for each current object. Eager loading requires that you retrieve all model objects at once via .all (instead of individually by .each). Eager loading can be cascaded, loading association's associated objects.

class Person < Sequel::Model
  one_to_many :posts, :eager=>[:tags]

class Post < Sequel::Model
  many_to_one :person
  one_to_many :replies
  many_to_many :tags

class Tag < Sequel::Model
  many_to_many :posts
  many_to_many :replies

class Reply < Sequel::Model
  many_to_one :person
  many_to_one :post
  many_to_many :tags

# Eager loading via .eager

# eager is a dataset method, so it works with filters/orders/limits/etc.
Post.filter{|o| o.topic > 'M'}.order(:date).limit(5).eager(:person).all

person = Person.first
# Eager loading via :eager (will eagerly load the tags for this person's posts)

# These are equivalent
Post.eager(:person, :tags).all

# Cascading via .eager

# Will also grab all associated posts' tags (because of :eager)

# No depth limit (other than memory/stack), and will also grab posts' tags
# Loads all people, their posts, their posts' tags, replies to those posts,
# the person for each reply, the tag for each reply, and all posts and
# replies that have that tag.  Uses a total of 8 queries.
Person.eager(:posts=>{:replies=>[:person, {:tags=>{:posts, :replies}}]}).all

In addition to using eager, you can also use eager_graph, which will use a single query to get the object and all associated objects. This may be necessary if you want to filter or order the result set based on columns in associated tables. It works with cascading as well, the syntax is exactly the same. Note that using eager_graph to eagerly load multiple *_to_many associations will cause the result set to be a cartesian product, so you should be very careful with your filters when using it in that case.

Extending the underlying dataset

The obvious way to add table-wide logic is to define class methods to the model class definition. That way you can define subsets of the underlying dataset, change the ordering, or perform actions on multiple records:

class Post < Sequel::Model
  def self.posts_with_few_comments
    filter{|o| o.num_comments < 30}

  def self.clean_posts_with_few_comments

You can also implement table-wide logic by defining methods on the dataset:

class Post < Sequel::Model
  def_dataset_method(:posts_with_few_comments) do
    filter{|o| o.num_comments < 30}

  def_dataset_method(:clean_posts_with_few_comments) do

This is the recommended way of implementing table-wide operations, and allows you to have access to your model API from filtered datasets as well:

Post.filter(:category => 'ruby').clean_posts_with_few_comments

Sequel models also provide a short hand notation for filters:

class Post < Sequel::Model
  subset(:posts_with_few_comments){|o| o.num_comments < 30}
  subset :invisible, ~:visible

Model Validations

You can define a validate method for your model, which #save will check before attempting to save the model in the database. If an attribute of the model isn't valid, you should add a error message for that attribute to the model object's errors. If an object has any errors added by the validate method, save will raise an error or return false depending on how it is configured.

class Post < Sequel::Model
  def validate
    errors.add(:name, "can't be empty") if name.empty?
    errors.add(:written_on, "should be in the past") if written_on >=