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

has_one association getting deleted on using create_association & validation fails #46737

Closed
akshaymohite opened this issue Dec 15, 2022 · 4 comments

Comments

@akshaymohite
Copy link
Contributor

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "~> 7.0.4"
  gem "byebug"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
  end

  create_table :accounts, force: true do |t|
    t.integer :user_id
  end
end

class User < ActiveRecord::Base
  has_one :account, dependent: :destroy
end

class Account < ActiveRecord::Base
  belongs_to :user

  validates_uniqueness_of :user_id
end

class BugTest < Minitest::Test
  def test_association_stuff
    user = User.create!
    assert_equal 1, User.count

    user.create_account!
    assert_equal 1, Account.count

    begin
      user.create_account!
    rescue ActiveRecord::RecordInvalid => e
      puts "Expected error raised because uniqueness violated - #{e.message}"
    end

    assert_equal 1, Account.count
  end
end

Expected behavior

The existing has_one relation from the database should not get deleted. The transaction should be rolled back.

Actual behavior

F

Failure:
BugTest#test_association_stuff [bug.rb:56]:
Expected: 1
  Actual: 0

System configuration

Rails version: 7.0.4

Ruby version: 3.1.2

More info

Tried debugging this and seems the associated record is getting deleted because of dependent: :destroy on User model. This method is being called which deletes this associated record. It seems intentional deletion but I am not sure if it should be the expected behavior in this case. Let me know if I am missing any context here.

@lazaronixon
Copy link
Contributor

lazaronixon commented Dec 22, 2022

Hey @s-mustafa, your test is passing on the main branch. Now we remove the old association before creating a new one, so in your case the exception will never be raised.

Probably fixed by #46386, #46790

gem "rails", github: "rails/rails", branch: "main"

@lazaronixon
Copy link
Contributor

@byroot I think it can be closed.

@byroot byroot closed this as completed Dec 22, 2022
@byroot byroot reopened this Dec 22, 2022
@byroot
Copy link
Member

byroot commented Dec 22, 2022

Actually, we might need to backport to 7.0-stable.

@byroot
Copy link
Member

byroot commented Dec 23, 2022

Backported in 83009f9

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

Successfully merging a pull request may close this issue.

3 participants