-
Notifications
You must be signed in to change notification settings - Fork 18
ActiveRecord::StatementInvalid when saving with association #8
Comments
Please share associations and unique constraints of |
Working on creating an isolated scenario... Not at the same place, but still seeing an issue.
Given the above setup, the below code returns a successful
The difference might that the code I mentioned in the original issue is running in rspec with DatabaseCleaner. Either way, the code in this example returning I believe we'll need to add a raise ActiveRecord::Rollback if running within a transaction. |
I've been testing out re-raising within
Where
Having the user returned in a state that appears to be persisted is very dangerous. |
FWIW, this issue isn't present when using sqlite. If you use postgres it is present. |
This is the default nature of ActiveRecord associations. The parent object will be saved to DB even if any child object fails validations. class User < ActiveRecord::Base
include RescueUniqueConstraint
rescue_unique_constraint index: :index_users_on_email, field: :email
has_one :profile, inverse_of: :user
validates_associated :profile # will ensure user is saved only when Profile is also valid
end More on Rails5: https://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html |
You're saying it is standard for Rails to return a User model that looks as if it is persisted but is not?
Note the User returned has an id (31), returns I know that is not default nature of ActiveRecord. When a record is rolled back, Rails acknowledges that by removing the primary key (id) value to communicate that it is not persisted. |
No In your case, you are saving two models at the same time. (A |
That sounds right but step 7 does not occur as you've noted. User saved to the database, had an id assigned to the model instance in memory, but rolled back after the profile save failed without rolling back the model changes to the user in memory leaving the id on the user. Sqlite works as you described but not postgres. I'll try to get a sample project pushed up this weekend to make it easier for you to run the code I had above and see the issue. |
I posted up a sample rails app to show the issue when using postgres. This did also confirm that the ActiveRecord::StatementInvalid comes from using using transactions around tests. |
I'm getting an error while using this gem when saving a model with built associations.
An example would be:
(not real but communicating the scenario of a
has_one
being built)rescue_unique_constraint
is on both the association (profile) and primary model (user) but the failure in this scenario is stemmed from the uniqueness failure on the association (profile).Has anybody ran into this before? Any suggestions?
Wondering if the rescue in the gem for the unique constraint should also re-raise with
ActiveRecord::Rollback
ifActiveRecord::Base.connection.open_transactions.positive?
is true?This might be related?
jeremyevans/sequel#908
The text was updated successfully, but these errors were encountered: