This project is archived and is in readonly mode.

#3004 ✓invalid
Adrien Pestel

has_many + foreign_key in ActiveRecord class not working

Reported by Adrien Pestel | August 7th, 2009 @ 04:12 PM

Hi,

class Entity < ActiveRecord::Base
set_table_name("glpi_entities") has_many :trackings, :class_name => "Tracking", :foreign_key => "FK_entities" end

class Tracking < ActiveRecord::Base
set_table_name("glpi_tracking") belongs_to :entity, :foreign_key => "FK_entities" belongs_to :user, :foreign_key => "author" end

In irb :
Entity.find(1).trackings return [] SQL : SELECT * FROM glpi_tracking WHERE (glpi_tracking.FK_entities = NULL);

Instead FK_entities in SQL should be set to "1"

Whereas Tracking.find(:first).entity works well

Regards,
Adrien

Comments and changes to this ticket

  • Adrien Pestel
  • CancelProfileIsBroken

    CancelProfileIsBroken August 8th, 2009 @ 12:12 PM

    • Tag changed from 2.3.3, :foreign_key to 2.3.3, :foreign_key, bugmash
  • David Trasbo

    David Trasbo August 8th, 2009 @ 03:51 PM

    I was unable to reproduce this bug in 2.3.3:

    The two models:

    @@@ Ruby class Entity < ActiveRecord::Base
    set_table_name 'glpi_entities' has_many :trackings, :class_name => 'Tracking', :foreign_key => 'FK_entities' end

    class Tracking < ActiveRecord::Base
    set_table_name 'glpi_tracking' belongs_to :entity, :foreign_key => 'FK_entities' end

    
    Console output:
    
    Loading development environment (Rails 2.3.3)

    entity = Entity.create => # 3.times { entity.trackings.create } => 3 entity.trackings => [#, #, #]

    
    This ticket may be marked as invalid.
    
  • David Trasbo

    David Trasbo August 8th, 2009 @ 03:58 PM

    Formatting fail.

    Models:

        class Entity < ActiveRecord::Base
          set_table_name 'glpi_entities'
          has_many :trackings, :class_name => 'Tracking', :foreign_key => 'FK_entities'
        end
    
        class Tracking < ActiveRecord::Base
          set_table_name 'glpi_tracking'
          belongs_to :entity, :foreign_key => 'FK_entities'
        end
    

    Console output:

        Loading development environment (Rails 2.3.3)
        >> entity = Entity.create
        => #<Entity id: 1, created_at: "2009-08-08 14:49:11", updated_at: "2009-08-08 14:49:11">
        >> 3.times { entity.trackings.create }
        => 3
        >> entity.trackings
        => [#<Tracking id: 1, FK_entities: 1, created_at: "2009-08-08 14:49:25", updated_at: "2009-08-08 14:49:25">, #<Tracking id: 2, FK_entities: 1, created_at: "2009-08-08 14:49:25", updated_at: "2009-08-08 14:49:25">, #<Tracking id: 3, FK_entities: 1, created_at: "2009-08-08 14:49:25", updated_at: "2009-08-08 14:49:25">]
    
  • Adrien Pestel

    Adrien Pestel August 8th, 2009 @ 11:58 PM

    The id field was setted up as uppercase in the original database...

    I've added set_primary_key :ID in each models.
    It works now.

    The ticket can be closed sorry for disturbing.

    Regards,
    Adrien

  • José Valim

    José Valim August 9th, 2009 @ 03:17 PM

    • State changed from “new” to “invalid”
    • Tag changed from 2.3.3, :foreign_key, bugmash to 2.3.3, :foreign_key

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages