Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record creation fails when one with_model has a foreign key pointing to another with_model #45

Open
dhnaranjo opened this issue Dec 21, 2023 · 0 comments

Comments

@dhnaranjo
Copy link

dhnaranjo commented Dec 21, 2023

I made you a lil repro script. The child without foreign key is created just fine, the child with foreign key hits an exception.

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "sqlite3"
  gem "combustion"
  gem "with_model"
  gem "minitest"
end

require "combustion"
require "minitest/autorun"
require "with_model"

# A hack to override some Combustion behavior
module Bundler
  def self.require(*)
    # noop
  end
end
ENV['DATABASE_URL'] = "sqlite3::memory:"
ENV['RAILS_ENV'] = "test"
Combustion.initialize! :active_record, database_reset: false, load_schema: false do
  config.enable_reloading = true
end

WithModel.runner = :minitest

class ForeignKeysSpec < Minitest::Spec
  extend WithModel

  with_model :Parent do
    model do
      has_many :children
      has_many :children_with_foreign_key, class_name: "ChildWithForeignKey"
    end
  end

  with_model :Child do
    table do |t|
      t.references :parent
    end

    model do
      belongs_to :parent
    end
  end

  with_model :ChildWithForeignKey do
    table do |t|
      t.references :parent, foreign_key: true
    end

    model do
      belongs_to :parent
    end
  end

  it "creates a child without a foreign key" do
    parent = Parent.create!
    child = parent.children.create!
    assert_equal parent, child.parent
  end

  it "creates a child with a foreign key" do
    parent = Parent.create!
    assert_raises ActiveRecord::StatementInvalid do
      parent.children_with_foreign_key.create!
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant