I've got two models Wallet and FakeWallet. The Wallet should have one FakeWallet and it should be identified through wallet_type and external_id. It means I need to add references based on wallets external_id field.
Standard migration will be:
# rails g migration addReferencesToFakeWallets wallet:references
class AddReferencesToFakeWallets < ActiveRecord::Migration[6.1]
def change
add_reference :fake_wallets, :wallet, null: false, foreign_key: true
end
end
After rails db:migrate produces me:
t.bigint "wallet_id", null: false
t.index ["wallet_id"], name: "index_fake_wallets_on_wallet_id"
How to bind these two models by external_id instead of wallet_id ?
[EDIT]
# schema.rb after suggestion from comments
create_table "fake_wallets", force: :cascade do |t|
t.decimal "balance", default: "0.0"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "external_id"
t.index ["external_id"], name: "index_fake_wallets_on_external_id"
end
create_table "wallets", force: :cascade do |t|
t.integer "wallet_type"
t.string "external_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end