Skip to content

Instantly share code, notes, and snippets.

@rohitn
Created June 14, 2014 00:23
Show Gist options
  • Save rohitn/782d225cdf0e70c696ee to your computer and use it in GitHub Desktop.
Save rohitn/782d225cdf0e70c696ee to your computer and use it in GitHub Desktop.
Sequel foreign keys
# 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