Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Support models having composite primary keys #37

Open
kakra opened this Issue May 3, 2013 · 4 comments

Comments

Projects
None yet
2 participants
Contributor

kakra commented May 3, 2013

I'm trying to use a table from a legacy DB which has no singular primary key. Instead I used the composite-primary-keys gem to use this table in Rails. When trying to use ranked-model, the SQL breaks (do not look too much at the ugly field names, that will be changed later when the legacy applications using the DB become obsolete):

irb(main):050:0> pos = Article.find(34129).topic_article_links.find_by_topicid(348)
  Article Load (0.6ms)  SELECT `topicarticles`.* FROM `topicarticles` WHERE `topicarticles`.`articleid` = 34129 LIMIT 1
  TopicArticleLink Load (0.5ms)  SELECT `group_topic_article`.* FROM `group_topic_article` WHERE `group_topic_article`.`articleid` = 34129 AND `group_topic_article`.`topicid` = 348 LIMIT 1
=> #<TopicArticleLink topicid: 348, articleid: 34129, status: "AKTIV", article_prio: 0.0>
irb(main):051:0> pos.article_order_position = 0
=> 0
irb(main):052:0> pos.save
   (0.5ms)  BEGIN
  TopicArticleLink Load (11.8ms)  SELECT `group_topic_article`.`id`, `group_topic_article`.`article_prio` FROM `group_topic_article` WHERE `group_topic_article`.`topic_id` IS NULL AND (`group_topic_article`.`id` != 348, 34129) ORDER BY `group_topic_article`.`article_prio` ASC LIMIT 1
   (0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'group_topic_article.id' in 'field list': SELECT  `group_topic_article`.`id`, `group_topic_article`.`article_prio` FROM `group_topic_article`  WHERE `group_topic_article`.`topic_id` IS NULL AND (`group_topic_article`.`id` != 348, 34129) ORDER BY `group_topic_article`.`article_prio` ASC LIMIT 1

TopicArticleLink is the join-model between Article and Topic. The simplified models look like this:

class TopicArticleLink < ActiveRecord::Base
  self.table_name = "group_topic_article"
  self.primary_keys = :topicid, :articleid

  belongs_to :topic, :foreign_key => "topicid"
  belongs_to :article, :foreign_key => "articleid"

  include RankedModel
  ranks :article_order, :column => :article_prio, :with_same => :topicid
end

class Article < ActiveRecord::Base
  self.table_name = "topicarticles"
  self.primary_key = "articleid"

  has_many :topic_article_links, :foreign_key => "articleid", :dependent => :destroy
  has_many :topics, :through => :topic_article_links
end

class Topic < ActiveRecord::Base
  self.primary_key = "topicid"
  has_many :topic_article_links, :foreign_key => "topicid", :dependent => :delete_all
  has_many :articles, :through => :topic_article_links
end
Owner

mixonic commented May 17, 2013

@kakra do your changes from #40 also fix the composite-keys gem use case?

Contributor

kakra commented May 17, 2013

@mixonic No not yet. Composite Keys use an array of primary keys and an array of ids, so it won't probably work with the way you interface to arel.

Contributor

kakra commented Jun 24, 2013

I wanted to start working on this but cannot due to this problem:

~/git/github/ranked-model [git:master] $ bundle
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Bundler could not find compatible versions for gem "rspec-mocks":
  In Gemfile:
    rspec-rails (~> 2.12.0) ruby depends on
      rspec-mocks (~> 2.12.0) ruby
    rspec (>= 0) ruby depends on
      rspec-mocks (2.13.1)
Contributor

kakra commented Aug 1, 2013

I won't dig into that because meanwhile I migrated the table to use a single primary key (luckily that did not crash our old legacy application which still works). That was easier. Still, you may want to keep this open so it can be added as a feature later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment