ActiveRecord::UnknownPrimaryKey being thrown after 3.1.1 -> 3.1.2 uprade #4504

Closed
marcbowes opened this Issue Jan 17, 2012 · 7 comments

Comments

Projects
None yet
4 participants
@marcbowes
Contributor

marcbowes commented Jan 17, 2012

I appear to have hit a regression. I two schemas in this website, the latter being an Invision Power Board forum.

I define models in app/models/ipb called forum and topic like so:

class Ipb::Forum < ActiveRecord::Base
  set_table_name "#{IPB_DB}.forums"

  has_many :topics, :class_name => "Ipb::Topic"
end

class Ipb::Topic < ActiveRecord::Base
  set_primary_key :tid
  set_table_name "#{IPB_DB}.topics"

  belongs_to :forum, :class_name => "Ipb::Forum"

  # .. elided ..
end

Under ActiveRecord 3.1.1, the following works: Ipb::Forum.first.topics. Under 3.1.2 it raises ActiveRecord::UnknownPrimaryKey. This, however works: Ipb::Topic.first.

I've taken only a brief look at it and it seems the issue comes in because klass.primary_key is being looked up on Forum instead of Topic. I will try and put together a repro project for this. Right now its a placeholder for me to do just that.

@emoreth

This comment has been minimized.

Show comment Hide comment
@emoreth

emoreth Feb 15, 2012

I had this same problem on a 3.1.2 app.
I am also using two schemas and defining set_table_name.

Problem fixed after I downgraded to 3.1.1.

emoreth commented Feb 15, 2012

I had this same problem on a 3.1.2 app.
I am also using two schemas and defining set_table_name.

Problem fixed after I downgraded to 3.1.1.

@carlosantoniodasilva

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva May 4, 2012

Owner

@marcbowes are you able to test this with latest 3.2 stable version? Thanks.

@marcbowes are you able to test this with latest 3.2 stable version? Thanks.

@marcbowes

This comment has been minimized.

Show comment Hide comment
@marcbowes

marcbowes May 7, 2012

Contributor

I will try tonight.

Contributor

marcbowes commented May 7, 2012

I will try tonight.

@marcbowes

This comment has been minimized.

Show comment Hide comment
@marcbowes

marcbowes May 8, 2012

Contributor

Looks to be fixed:

With Rails 3.1.4:

$ bundle show rails
/home/marc/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/rails-3.1.4
$ rails console
Loading development environment (Rails 3.1.4)
irb(main):001:0> Ipb::Topic.first
  Ipb::Topic Load (0.4ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` LIMIT 1
=> #<Ipb::Topic tid: 1, title: "Welcome", ... elided ... >
irb(main):002:0> Ipb::Forum.first.topics
  Ipb::Forum Load (0.4ms)  SELECT `invision_power_board`.`forums`.* FROM `invision_power_board`.`forums` LIMIT 1
  Ipb::Topic Load (0.6ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` WHERE `invision_power_board`.`topics`.`forum_id` = 3
=> []

Now after changing Gemfile to use ~> 3.2.0:

$ bundle update rails
... elided ...
$ bundle show rails
/home/marc/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/rails-3.2.3
$ rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> Ipb::Topic.first
  Ipb::Topic Load (0.2ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` LIMIT 1
=> #<Ipb::Topic tid: 1, title: "Welcome", ... elided ...>
irb(main):002:0> Ipb::Forum.first.topics
  Ipb::Forum Load (0.2ms)  SELECT `invision_power_board`.`forums`.* FROM `invision_power_board`.`forums` LIMIT 1
  Ipb::Topic Load (0.1ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` WHERE `invision_power_board`.`topics`.`forum_id` = 3
=> []
Contributor

marcbowes commented May 8, 2012

Looks to be fixed:

With Rails 3.1.4:

$ bundle show rails
/home/marc/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/rails-3.1.4
$ rails console
Loading development environment (Rails 3.1.4)
irb(main):001:0> Ipb::Topic.first
  Ipb::Topic Load (0.4ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` LIMIT 1
=> #<Ipb::Topic tid: 1, title: "Welcome", ... elided ... >
irb(main):002:0> Ipb::Forum.first.topics
  Ipb::Forum Load (0.4ms)  SELECT `invision_power_board`.`forums`.* FROM `invision_power_board`.`forums` LIMIT 1
  Ipb::Topic Load (0.6ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` WHERE `invision_power_board`.`topics`.`forum_id` = 3
=> []

Now after changing Gemfile to use ~> 3.2.0:

$ bundle update rails
... elided ...
$ bundle show rails
/home/marc/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/rails-3.2.3
$ rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> Ipb::Topic.first
  Ipb::Topic Load (0.2ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` LIMIT 1
=> #<Ipb::Topic tid: 1, title: "Welcome", ... elided ...>
irb(main):002:0> Ipb::Forum.first.topics
  Ipb::Forum Load (0.2ms)  SELECT `invision_power_board`.`forums`.* FROM `invision_power_board`.`forums` LIMIT 1
  Ipb::Topic Load (0.1ms)  SELECT `invision_power_board`.`topics`.* FROM `invision_power_board`.`topics` WHERE `invision_power_board`.`topics`.`forum_id` = 3
=> []

@marcbowes marcbowes closed this May 8, 2012

@carlosantoniodasilva

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva May 8, 2012

Owner

@marcbowes great, thanks for reporting back.

@marcbowes great, thanks for reporting back.

@whistlerbrk

This comment has been minimized.

Show comment Hide comment
@whistlerbrk

whistlerbrk Jan 22, 2013

Contributor

During an upgrade from Rails 3.0.4 to 3.2.11 (3-2 stable) this issue has come up for me.

I am working on an app who has model names "Index" (indices") and IndexCategories and am somewhat concerned that I may have been snagged onto a problem with a reserved word.

# == Schema Information
#
# Table name: indices
#
#  id     :integer          not null, primary key
#  dl_id :string(6)        not null
#  name   :string(255)      not null
#

class Index < ActiveRecord::Base
  has_many :index_categories

  def self.default
    ic = IndexCategory.find_by_category("Fund")
    ic.index  # THIS LINE FAILS
  end
end

# == Schema Information
#
# Table name: index_categories
#
#  id             :integer          not null, primary key
#  index_id       :integer
#  category       :string(255)
#  category_id    :integer
#  name           :string(255)
#

class IndexCategory < ActiveRecord::Base
  belongs_to :index
  has_many :category_stats, :foreign_key => [:category, :category_id], :primary_key => [:category, :category_id]
  validates_presence_of :index, :category, :category_id
  validates_uniqueness_of :category, :scope => :category_id
end

I am returned: ActiveRecord::UnknownPrimaryKey Unknown primary key for table indices in model Index.

Could this issue even remotely be related?
#8414

Contributor

whistlerbrk commented Jan 22, 2013

During an upgrade from Rails 3.0.4 to 3.2.11 (3-2 stable) this issue has come up for me.

I am working on an app who has model names "Index" (indices") and IndexCategories and am somewhat concerned that I may have been snagged onto a problem with a reserved word.

# == Schema Information
#
# Table name: indices
#
#  id     :integer          not null, primary key
#  dl_id :string(6)        not null
#  name   :string(255)      not null
#

class Index < ActiveRecord::Base
  has_many :index_categories

  def self.default
    ic = IndexCategory.find_by_category("Fund")
    ic.index  # THIS LINE FAILS
  end
end

# == Schema Information
#
# Table name: index_categories
#
#  id             :integer          not null, primary key
#  index_id       :integer
#  category       :string(255)
#  category_id    :integer
#  name           :string(255)
#

class IndexCategory < ActiveRecord::Base
  belongs_to :index
  has_many :category_stats, :foreign_key => [:category, :category_id], :primary_key => [:category, :category_id]
  validates_presence_of :index, :category, :category_id
  validates_uniqueness_of :category, :scope => :category_id
end

I am returned: ActiveRecord::UnknownPrimaryKey Unknown primary key for table indices in model Index.

Could this issue even remotely be related?
#8414

@whistlerbrk

This comment has been minimized.

Show comment Hide comment
@whistlerbrk

whistlerbrk Jan 22, 2013

Contributor

And... eating my words 5 minutes later.. The table definition prepending by the annotate gem is incorrect. This is a legacy system with a composite primary key (id, dl_id).

ALTER TABLE indices MODIFY id INT NOT NULL;
ALTER TABLE indices DROP PRIMARY KEY;
ALTER TABLE indices MODIFY id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;

Fixed my issue, I will report the incorrect annotation, though.. composite primary keys.. really?

Contributor

whistlerbrk commented Jan 22, 2013

And... eating my words 5 minutes later.. The table definition prepending by the annotate gem is incorrect. This is a legacy system with a composite primary key (id, dl_id).

ALTER TABLE indices MODIFY id INT NOT NULL;
ALTER TABLE indices DROP PRIMARY KEY;
ALTER TABLE indices MODIFY id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;

Fixed my issue, I will report the incorrect annotation, though.. composite primary keys.. really?

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