Created
June 14, 2014 00:23
-
-
Save rohitn/782d225cdf0e70c696ee to your computer and use it in GitHub Desktop.
Sequel foreign keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/jeremyevans/sequel/issues/830 | |
require 'sequel' | |
require 'logger' | |
DB = Sequel.sqlite | |
logger = Logger.new($stdout) | |
DB.loggers << logger | |
DB.create_table :channels do | |
String :id, :primary_key => true | |
String :title, :null => false | |
end | |
class Channel < Sequel::Model | |
unrestrict_primary_key | |
one_to_many :videos | |
end | |
DB.create_table :videos do | |
primary_key :id | |
String :title, :null => false | |
foreign_key :channel_id, :channels, type: String | |
end | |
class Video < Sequel::Model | |
many_to_one :channel | |
end | |
channel = Channel.create(id: 'testId', title: 'testTitle') | |
channel.add_video(title: 'testTitle') | |
logger.info "Channel id: #{channel.id}" | |
logger.info "Video channel id: #{Video.first.channel_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment