Home > Mobile >  Why am I getting ActiveRecord::Fixture::FixtureError error when loading name-spaced fixtures?
Why am I getting ActiveRecord::Fixture::FixtureError error when loading name-spaced fixtures?

Time:05-24

I am getting the following error ActiveRecord::Fixture::FixtureError: table "customers_database_configs" has no columns named "customers_database_cluster". when I try rails db:fixtures:load. I believe it is due to the name-spacing of the models but I can't for the life of me figure out how to fix the issue. Using Rails 6.1.5.1, I have included the relevant files below including schema, migrations, models and fixtures.

Below is the relevant part of the schema:

create_table "customers_database_configs", force: :cascade do |t|
    t.string "hostname"
    t.integer "port"
    t.string "database"
    t.string "schema"
    t.string "db_username"
    t.string "db_password"
    t.boolean "active"
    t.datetime "valid_until"
    t.boolean "refresh_database"
    t.string "storage_bucket"
    t.string "storage_username"
    t.string "storage_password"
    t.string "timezone"
    t.json "data"
    t.string "account_uid"
    t.bigint "customers_database_cluster_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["customers_database_cluster_id"], name: "customers_database_cluster_id"
  end

Below is the relevant part of the migrations:

class CreateCustomersDatabaseClusters < ActiveRecord::Migration[6.1]
  def change
    create_table :customers_database_clusters do |t|
      t.string :hostname
      t.integer :port
      t.string :database
      t.string :su_username
      t.string :su_password

      t.timestamps
    end
  end
end
class CreateCustomersDatabaseConfigs < ActiveRecord::Migration[6.1]
  def change
    create_table :customers_database_configs do |t|
      t.string :account_uid
      t.integer :port
      t.string :database
      t.string :schema
      t.string :db_username
      t.string :db_password
      t.boolean :active
      t.datetime :valid_until
      t.boolean :refresh_database
      t.string :storage_bucket
      t.string :storage_username
      t.string :storage_password
      t.string :timezone
      t.json :data
      t.string :account_uid
      t.references :customers_database_cluster, index: { name: :customers_database_cluster_id }

      t.timestamps
    end
  end
end

Below is the relevant part of the models:

#app/models/customers_database/config.rb
module CustomersDatabase
  class Config < ApplicationRecord
    belongs_to :cluster
  end
end

#app/models/customers_database/cluster.rb
module CustomersDatabase
    class Cluster < ApplicationRecord
        has_many :configs
    end

end

#app/models/customers_database.rb
module CustomersDatabase
  def self.table_name_prefix
    'customers_database_'
  end
end


and fixtures:

#test/fixtures/customers_database/clusters.yml
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
  port: 1
  hostname: MyString
  database: MyString
  su_username: MyString
  su_password: MyString

two:
  port: 1
  hostname: MyString
  database: MyString
  su_username: MyString
  su_password: MyString
#test/fixtures/customers_database/configs.yml
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
  hostname: MyString
  port: 1
  database: MyString
  schema: MyString
  db_username: MyString
  db_password: MyString
  active: false
  valid_until: 2022-05-23 17:21:17
  refresh_database: false
  storage_bucket: MyString
  storage_username: MyString
  storage_password: MyString
  timezone: MyString
  data: 
  customers_database_cluster: one

two:
  hostname: MyString
  port: 1
  database: MyString
  schema: MyString
  db_username: MyString
  db_password: MyString
  active: false
  valid_until: 2022-05-23 17:21:17
  refresh_database: false
  storage_bucket: MyString
  storage_username: MyString
  storage_password: MyString
  timezone: MyString
  data: 
  customers_database_cluster: two

CodePudding user response:

belongs_to association is not set up properly:

>> CustomersDatabase::Config.create!(cluster: CustomersDatabase::Cluster.create!)
...
Traceback (most recent call last):
        1: from (irb):1
ActiveModel::MissingAttributeError (can't write unknown attribute `cluster_id`)

By default, the foreign_key is inferred from the name of the association:

>> CustomersDatabase::Config.reflect_on_association(:cluster).foreign_key
=> "cluster_id"

foreign_key option can be specified on belongs_to to match the database column:

module CustomersDatabase
  class Config < ApplicationRecord
    belongs_to :cluster, foreign_key: :customers_database_cluster_id
  end
end

This fixes the association:

>> CustomersDatabase::Config.create!(cluster: CustomersDatabase::Cluster.create!)
=> #<CustomersDatabase::Config:0x00007f5f5adac9e0>

In fixtures, you have to reference an existing column or association:

# test/fixtures/customers_database/clusters.yml
cluster_one:
  port: 1
  hostname: MyString
  database: MyString
  su_username: MyString
  su_password: MyString

# test/fixtures/customers_database/configs.yml
config_one:
  # hostname: MyString <= that shouldn't be here
  port: 1
  database: MyString
  schema: MyString
  db_username: MyString
  db_password: MyString
  active: false
  valid_until: 2022-05-23 17:21:17
  refresh_database: false
  storage_bucket: MyString
  storage_username: MyString
  storage_password: MyString
  timezone: MyString
  data:
  # NOTE: there is no column or association named "customers_database_cluster"
  # customers_database_cluster: one
  cluster: cluster_one

Fixtures should load now:

$ bin/rails db:fixtures:load 
Running via Spring preloader in process 4098254

Another solution is to change the migration and rename reference from "customers_database_cluster" to "cluster". This will create cluster_id column that will be used by belongs_to :cluster:

class CreateCustomersDatabaseConfigs < ActiveRecord::Migration[6.1]
  def change
    create_table :customers_database_configs do |t|
      t.string :account_uid
      t.integer :port
      t.string :database
      t.string :schema
      t.string :db_username
      t.string :db_password
      t.boolean :active
      t.datetime :valid_until
      t.boolean :refresh_database
      t.string :storage_bucket
      t.string :storage_username
      t.string :storage_password
      t.string :timezone
      t.json :data
      # t.string :account_uid <= already defined at the very top

      # NOTE: rename the reference to play nice with association in Config model
      #       foreign_key is optional but is usually good to have.
      # t.references :customers_database_cluster, index: { name: :customers_database_cluster_id }
      t.references :cluster, foreign_key: { to_table: :customers_database_clusters }

      t.timestamps
    end
  end
end
module CustomersDatabase
  class Config < ApplicationRecord
    belongs_to :cluster
  end
end
$ bin/rails db:fixtures:load
Running via Spring preloader in process 4099322

https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

https://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_association

  • Related