On Mar 30, 6:44 am, morbusg <[email protected]> wrote:
> How are they meant to be defined? I found one topic on it dating back
> to 2008, so maybe some things have changed in between:
>
> # BEGIN
> require 'rubygems'
> require 'sequel'
> require 'logger'
>
> DB = Sequel.sqlite
> DB.loggers << Logger.new(STDOUT)
> Sequel::Model.plugin(:schema)
>
> class Foo < Sequel::Model
>   set_schema do
>     primary_key :title, :type => :text, :auto_increment => false
>   end and create_table unless table_exists?
>
>   one_to_many :bars
> end
>
> class Bar < Sequel::Model
>   set_schema do
>     primary_key :name, :type => :text, :auto_increment => false
>     foreign_key :foo_title, :foos, :type => :text
>   end and create_table unless table_exists?
>
>   many_to_one :foo, :key => :foo_title
> end
>
> Foo.insert(:title => 'Baz')
> Foo.first.bars
> # END
>
> => SELECT * FROM `bars` WHERE (`bars`.`foo_id` = 'Baz')

The primary_key method sets up auto incrementing (surrogate) keys.  To
use a single natural primary key, you want the :primary_key option:

  class Foo < Sequel::Model
    set_schema do
      String :title, :text => :true, :primary_key => true
    end
    create_table?
  end

For composite natural primary keys, you pass the primary_key method an
array:

  class Foo < Sequel::Model
    set_schema do
      String :title, :text => :true
      String :blah, :text => :true
      primary_key [:title, :blah]
    end
    create_table?
  end

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to