为什么我在加载名称空间装置时出现 ActiveRecord::Fixture::FixtureError 错误?
Why am I getting ActiveRecord::Fixture::FixtureError error when loading name-spaced fixtures?
我在尝试 rails db:fixtures:load
时遇到以下错误 ActiveRecord::Fixture::FixtureError: table "customers_database_configs" has no columns named "customers_database_cluster".
。我相信这是由于模型的名称间距所致,但我终生无法弄清楚如何解决该问题。 Using Rails 6.1.5.1
,我在下面包含了相关文件,包括架构、迁移、模型和固定装置。
以下是架构的相关部分:
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
以下是迁移的相关部分:
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
以下是模型的相关部分:
#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
和赛程:
#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
belongs_to
关联设置不正确:
>> 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`)
默认情况下,foreign_key 是从关联名称推断的:
>> CustomersDatabase::Config.reflect_on_association(:cluster).foreign_key
=> "cluster_id"
foreign_key 可以在 belongs_to
上指定选项以匹配数据库列:
module CustomersDatabase
class Config < ApplicationRecord
belongs_to :cluster, foreign_key: :customers_database_cluster_id
end
end
这修复了关联:
>> CustomersDatabase::Config.create!(cluster: CustomersDatabase::Cluster.create!)
=> #<CustomersDatabase::Config:0x00007f5f5adac9e0>
在灯具中,您必须引用现有的列或关联:
# 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
灯具现在应该加载:
$ bin/rails db:fixtures:load
Running via Spring preloader in process 4098254
另一个解决方案是将迁移和重命名引用从“customers_database_cluster”更改为“集群”。这将创建 cluster_id 列,供 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
我在尝试 rails db:fixtures:load
时遇到以下错误 ActiveRecord::Fixture::FixtureError: table "customers_database_configs" has no columns named "customers_database_cluster".
。我相信这是由于模型的名称间距所致,但我终生无法弄清楚如何解决该问题。 Using Rails 6.1.5.1
,我在下面包含了相关文件,包括架构、迁移、模型和固定装置。
以下是架构的相关部分:
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
以下是迁移的相关部分:
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
以下是模型的相关部分:
#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
和赛程:
#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
belongs_to
关联设置不正确:
>> 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`)
默认情况下,foreign_key 是从关联名称推断的:
>> CustomersDatabase::Config.reflect_on_association(:cluster).foreign_key
=> "cluster_id"
foreign_key 可以在 belongs_to
上指定选项以匹配数据库列:
module CustomersDatabase
class Config < ApplicationRecord
belongs_to :cluster, foreign_key: :customers_database_cluster_id
end
end
这修复了关联:
>> CustomersDatabase::Config.create!(cluster: CustomersDatabase::Cluster.create!)
=> #<CustomersDatabase::Config:0x00007f5f5adac9e0>
在灯具中,您必须引用现有的列或关联:
# 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
灯具现在应该加载:
$ bin/rails db:fixtures:load
Running via Spring preloader in process 4098254
另一个解决方案是将迁移和重命名引用从“customers_database_cluster”更改为“集群”。这将创建 cluster_id 列,供 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