Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3242

Inconsistent foreign_key on association metadata

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None

      I ran into a problem where a

      Unable to find source-code formatter for language: has_many. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       / belongs_to

      relation only works one way. It turned out that when querying the child from the parent, the wrong foreign_key is used. This is what my relations look like:

      class Ontology::Job
      
        include Mongoid::Document
      
        has_many :ontology_synonyms, :class_name => 'Ontology::Synonym'
      
      end
      
      class Ontology::Synonym
      
        include Mongoid::Document
      
        belongs_to :ontology_job, :class_name => 'Ontology::Job'
      
      end
      

      I expect the foreign key on the child to be

      Unable to find source-code formatter for language: ontology_job_id```. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       since this is what derives from the relation name, but it queries 

      job_id

       instead.
      
      

      irb
      [36] pry(main)> oj = Ontology::Job.first
      MOPED: 127.0.0.1:27017 QUERY database=ontology_development collection=ontology_jobs selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (0.9291ms)
      [37] pry(main)> oj.ontology_synonyms
      MOPED: 127.0.0.1:27017 QUERY database=ontology_development collection=ontology_synonyms selector=

      {"job_id"=>11000002}

      flags=[] limit=0 skip=0 batch_size=nil fields=nil (53.1111ms)
      => []

      
      The relations metadata contains different foreign_keys depending on the direction queried. I expected them to both be 

      ontology_job_id

      .
      
      

      irb
      [23] pry(main)> Ontology::Synonym.reflect_on_association(:ontology_job).foreign_key
      => "ontology_job_id"
      [24] pry(main)> Ontology::Job.reflect_on_association(:ontology_synonyms).foreign_key
      => "job_id"
      `

            Assignee:
            arthurnn Arthur Nogueira Neves
            Reporter:
            masone Christian Felder
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: