代码之家  ›  专栏  ›  技术社区  ›  OKMantis

mysql2::错误:MSQL表为外语的未知列

  •  0
  • OKMantis  · 技术社区  · 5 年前

    我已经构建了一个Rails应用程序,它连接到一个MySQL数据库,该数据库以西班牙语作为表名和列。我已经制作了用英文名称表示表格的模型,并将其放入每个模型中。 self.table_name = "table_name_in_spanish" 将表转换为模型名,这样就不会破坏Rails约定。考虑到这一点,我有一个模型 Ad 一个模型 AdCopy 其中包含对 广告 模型。当我想为一个广告获得这些描述的列表时,我会在里面做以下操作 rails c :

    我首先将AD分配给变量:

    pry(main)> ad = Ad.last
      Ad Load (0.4ms)  SELECT  `anuncios`.* FROM `anuncios` ORDER BY `anuncios`.`id` DESC LIMIT 1
    => #<Ad:0x00007ff8dc45e2f0
     id: 1241,
     empresa: "Example Ad",
     tel: "555 555 555",
     [...]
    

    然后尝试访问广告副本(描述属于广告):

    pry(main)> ad.ad_copies
      AdCopy Load (1.5ms)  SELECT `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241
      AdCopy Load (1.3ms)  SELECT  `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 LIMIT 11
    => #<AdCopy::ActiveRecord_Associations_CollectionProxy:0x3ffc6d8fc498>
    

    我在这里的东西已经让我困惑了,因为我不知道 ActiveRecord_Associations_CollectionProxy 是。

    因此,最后一步是尝试获取要显示的第一个描述:

    pry(main)> ad.ad_copies.first
      AdCopy Load (1.3ms)  SELECT  `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 ORDER BY `anuncios_textos`.`id_anuncio` ASC LIMIT 1
    ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'anuncios_textos.ad_id' in 'where clause': SELECT  `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 ORDER BY `anuncios_textos`.`id_anuncio` ASC LIMIT 1
    from /Users/okmantis/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
    

    问题就在这里。我如何能够访问属于我的广告的描述?

    以下是我的模型:

    class Ad < ApplicationRecord
      self.table_name = "anuncios"
      has_many :ad_copies
      has_many :ad_addresses
      has_many :relationships, foreign_key: 'id_anuncio'
      has_many :magazines
      has_many :categories, through: :relationships
      belongs_to :user
    end
    
    class AdCopy < ApplicationRecord
      self.table_name = "anuncios_textos"
      self.primary_key = "id_anuncio"
      belongs_to :ad, foreign_key: "id_anuncio", optional: true
      belongs_to :language, foreign_key: "id_idioma", optional: true
    end
    

    模型ad的模式表(西班牙语:anuncios):

    create_table "anuncios", id: :integer, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8", force: :cascade do |t|
        t.string "empresa", null: false
        t.string "tel", null: false
        t.string "fax_principal", null: false
        t.string "movil_principal", null: false
        t.string "email_principal", null: false
        t.string "web", null: false
        t.string "facebook", null: false
        t.string "horario_v_principal", null: false
        t.string "horario_i_principal", null: false
        t.string "direccion_principal", null: false
        t.string "poblacion_principal", null: false
        t.string "activo", limit: 2, null: false
        t.string "tam_anuncio", null: false
        t.string "twitter", null: false
        t.string "link", limit: 2, null: false
        t.string "general", limit: 2, null: false
        t.string "isla", limit: 10, null: false
        t.string "subtitulo", null: false
        t.string "comentario", null: false
        t.datetime "modificacion", null: false
        t.integer "promo1", default: 0, null: false
        t.integer "promo2", default: 0, null: false
        t.string "instagram", null: false
        t.string "tel2", null: false
        t.string "tel3", null: false
        t.string "tel4", null: false
        t.string "movil2", null: false
        t.string "movil3", null: false
        t.string "movil4", null: false
      end
    

    模型adcopy的模式表(西班牙语:anuncios-textos):

    create_table "anuncios_textos", primary_key: ["id_anuncio", "id_idioma"], options: "ENGINE=MyISAM DEFAULT CHARSET=utf8", force: :cascade do |t|
        t.integer "id_anuncio", null: false
        t.integer "id_idioma", null: false
        t.text "descripcion", null: false
      end
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   OKMantis    5 年前

    我找到了答案。我忘了将广告模型中的外键设置为“id_anuncio”。如果您没有明确地告诉Rails外键列的名称不同,那么它只会通过模型名称+ID来调用它: ad_id . 您可以在控制台内部的错误中看到这一点: WHERE anuncios_textos.ad_id .

    因此,我的广告模式必须具备以下条件:

    class Ad < ApplicationRecord
      self.table_name = "anuncios"
      has_many :ad_copies, foreign_key: 'id_anuncio'
      [...]
    end