Ok!

Her is an example with migrations that doesn't work:

-------------------------------------------------------------------------
require 'rubygems'
require "sequel"
require "logger"

DB = Sequel.sqlite '', :logger => [Logger.new($stdout)]

class CreateBp < Sequel::Migration
  def up
    create_table :bps do
      primary_key :id
      text :name
    end
  end
end

class CreateStatistic < Sequel::Migration
  def up
    create_table :statistics do
      primary_key :id
      foreign_key :bp_id, :table => :bps
      integer :waiting
    end
  end
end

class Bp < Sequel::Model
   set_schema do
      primary_key :id
      text :name
    end
  one_to_many :statistics
end

class Statistic < Sequel::Model
   set_schema do
      primary_key :id
      integer :waiting
      foreign_key :bp_id, :table => :bps
  end
  many_to_one :bp
end

# CreateBp.apply(DB, :up)
# CreateStatistic.apply(DB,:up)

Bp.create_table!
Statistic.create_table!

bp = Bp.create(:name => "Order")
  statistic = Statistic.create(:waiting => rand*100)
   bp.add_statistic(statistic)

bp.statistics.each do |stat|
  puts "stat: " + stat[:waiting].to_s
end
---------------------------------------------------
Failes with /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
record.rb:153:in `pk': No primary key is associated with this model
(Sequel::Error)
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
record.rb:356:in `add_associated_object'
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
associations.rb:275:in `add_statistic'
        from /home/morten/projects/rubyworkspace/main/Sinatra/sequel_test.rb:
51

If I use set_primary_key manually (set_primary_key :id)  on the
objects, I get this error:
/usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
associations.rb:448:in `send': undefined method `bp_id=' for
#<Statistic @values={:waiting=>12, :bp_id=>nil, :id=>1}>
(NoMethodError)
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
associations.rb:448:in `_add_statistic'
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
record.rb:359:in `send'
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
record.rb:359:in `add_associated_object'
        from /usr/lib/ruby/gems/1.8/gems/sequel-2.7.1/lib/sequel_model/
associations.rb:275:in `add_statistic'
        from /home/morten/projects/rubyworkspace/main/Sinatra/sequel_test.rb:
53

Putting the schema on the object works:
------------------------------------------------------------
require 'rubygems'
require "sequel"
require "logger"

DB = Sequel.sqlite '', :logger => [Logger.new($stdout)]

class Bp < Sequel::Model
   set_schema do
      primary_key :id
      text :name
    end
  one_to_many :statistics
end

class Statistic < Sequel::Model
   set_schema do
      primary_key :id
      integer :waiting
      foreign_key :bp_id, :table => :bps
  end
  many_to_one :bp
end

Bp.create_table!
Statistic.create_table!

bp = Bp.create(:name => "Order")
  statistic = Statistic.create(:waiting => rand*100)
   bp.add_statistic(statistic)

bp.statistics.each do |stat|
  puts "stat: " + stat[:waiting].to_s
end
------------------------------------------------

Using both sides of the two-way relation works fine:
   statistic.bp(bp)

With Migration this gives the same error as above.

Best regards,
Morten.
--~--~---------~--~----~------------~-------~--~----~
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