Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many + foreign_key in ActiveRecord class not working

#3004

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

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Adrien Pestel
  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.3, :foreign_key to 2.3.3, :foreign_key, bugmash

    August 8th, 2009 @ 12:12 PM

  3. David Trasbo
    David Trasbo

    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.
    

    August 8th, 2009 @ 03:51 PM

  4. David Trasbo
    David Trasbo

    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">]
    

    August 8th, 2009 @ 03:58 PM

  5. Adrien Pestel
    Adrien Pestel

    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

    August 8th, 2009 @ 11:58 PM

  6. José Valim
    José Valim
    • State changed from new to invalid
    • Tag changed from 2.3.3, :foreign_key, bugmash to 2.3.3, :foreign_key

    August 9th, 2009 @ 03:17 PM