Last active
December 16, 2015 10:09
-
-
Save letronje/5418354 to your computer and use it in GitHub Desktop.
pg table inheritance
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
=begin | |
Gemfile: | |
activerecord-postgresql-extensions | |
=end | |
#db/migrate/create_answers.rb | |
class CreateAnswers < ActiveRecord::Migration | |
def change | |
create_table :answers do |t| | |
t.string :desc | |
t.timestamps | |
end | |
end | |
end | |
#db/migrate/create_special_answers.rb | |
class CreateSpecialAnswers < ActiveRecord::Migration | |
def self.up | |
create_table :special_answers, :inherits => :answers do |t| | |
#t.string :desc | |
t.string :misc | |
#t.timestamps | |
end | |
#execute "ALTER TABLE special_answers INHERIT answers" | |
end | |
def self.down | |
drop_table :special_answers | |
end | |
end | |
#app/models/answer.rb | |
class Answer < ActiveRecord::Base | |
attr_accessible :desc | |
#default_scope :select => "answers.*, id, tableoid::regclass table_name" | |
end | |
#app/models/special_answer.rb | |
class SpecialAnswer < Answer | |
set_table_name :special_answers | |
attr_accessible :misc | |
end | |
#Queries | |
SpecialAnswer.create(:misc => "Special", :desc => "Special Desc") | |
SpecialAnswer.create(:misc => "Special 2", :desc => "Special Desc 2") | |
Answer.create(:desc => "Normal Desc 3") | |
Answer.create(:desc => "Normal Desc") | |
ap Answer.all | |
[ | |
[0] #<Answer:0xb424180> { | |
:id => 2, | |
:desc => "Normal Desc", | |
:created_at => Fri, 19 Apr 2013 05:39:54 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:54 UTC +00:00 | |
}, | |
[1] #<Answer:0xb423e74> { | |
:id => 3, | |
:desc => "Normal Desc 3", | |
:created_at => Fri, 19 Apr 2013 05:39:57 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:57 UTC +00:00 | |
}, | |
[2] #<Answer:0xb423b68> { | |
:id => 4, | |
:desc => "Special Desc", | |
:created_at => Fri, 19 Apr 2013 05:39:38 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:38 UTC +00:00 | |
}, | |
[3] #<Answer:0xb42385c> { | |
:id => 5, | |
:desc => "Special Desc 2", | |
:created_at => Fri, 19 Apr 2013 05:39:41 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:41 UTC +00:00 | |
} | |
] | |
ap SpecialAnswer.all | |
[ | |
[0] #<SpecialAnswer:0xb382ec0> { | |
:id => 4, | |
:desc => "Special Desc", | |
:created_at => Fri, 19 Apr 2013 05:39:38 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:38 UTC +00:00, | |
:misc => "Special" | |
}, | |
[1] #<SpecialAnswer:0xb382bb4> { | |
:id => 5, | |
:desc => "Special Desc 2", | |
:created_at => Fri, 19 Apr 2013 05:39:41 UTC +00:00, | |
:updated_at => Fri, 19 Apr 2013 05:39:41 UTC +00:00, | |
:misc => "Special 2" | |
} | |
] | |
=begin | |
pgtest_development=# \d | |
List of relations | |
Schema | Name | Type | Owner | |
--------+------------------------+----------+---------- | |
public | answers | table | postgres | |
public | answers_id_seq | sequence | postgres | |
public | schema_migrations | table | postgres | |
public | special_answers | table | postgres | |
public | special_answers_id_seq | sequence | postgres | |
(5 rows) | |
pgtest_development=# \d answers | |
Table "public.answers" | |
Column | Type | Modifiers | |
------------+-----------------------------+------------------------------------------------------ | |
id | integer | not null default nextval('answers_id_seq'::regclass) | |
desc | character varying(255) | | |
created_at | timestamp without time zone | not null | |
updated_at | timestamp without time zone | not null | |
Indexes: | |
"answers_pkey" PRIMARY KEY, btree (id) | |
Number of child tables: 1 (Use \d+ to list them.) | |
pgtest_development=# \d+ answers | |
Table "public.answers" | |
Column | Type | Modifiers | Storage | Stats target | Description | |
------------+-----------------------------+------------------------------------------------------+----------+--------------+------------- | |
id | integer | not null default nextval('answers_id_seq'::regclass) | plain | | | |
desc | character varying(255) | | extended | | | |
created_at | timestamp without time zone | not null | plain | | | |
updated_at | timestamp without time zone | not null | plain | | | |
Indexes: | |
"answers_pkey" PRIMARY KEY, btree (id) | |
Child tables: special_answers | |
Has OIDs: no | |
pgtest_development=# \d special_answers | |
Table "public.special_answers" | |
Column | Type | Modifiers | |
------------+-----------------------------+-------------------------------------------------------------- | |
id | integer | not null default nextval('special_answers_id_seq'::regclass) | |
desc | character varying(255) | | |
created_at | timestamp without time zone | not null | |
updated_at | timestamp without time zone | not null | |
misc | character varying(255) | | |
Indexes: | |
"special_answers_pkey" PRIMARY KEY, btree (id) | |
Inherits: answers | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment