rails/rails — pull request #10417 by jholton (2013-05-02 08:37:39+00:00)
Title: autosave_association issue that occurs when table has unique index (resubmission)

This is a resubmission of #10183.

#10183 was merged and subsequently unmerged due to test failing for MySQL.
Comments [here](https://github.com/rails/rails/commit/e8727d37fc49d5bf9976c3cb5c46badb92cf4ced#commitcomment-3047920)

For this one, I have incorporated the changes suggested by @rafaelfranca in c0a07af.
This change however still changes the schema inside the test and works around that by setting `use_transactional_fixtures` to `false` no matter which connection adapter is in use for that test class.

Is this OK or would it be preferable for me to alter the actual schema (and corresponding models) by adding a unique index on a table there?
I started down this path, but it quickly became more complex than I hoped (i.e. changing the existing schema breaking unrelated test). I'll work through it though, if that is preferable.
cc @jonleighton

Thanks.

From original #10183 description:
"Per the comments in pull #3329,
If you have a table with a unique field index, and you mark a record for destruction, and you build a new record with the same value as the unique field, then when you call `save`, a database level unique index error will be thrown.

This happens because the record destruction happens after record creation.

In this pull request, I moved the record destruction ahead of the record creation.

This is my first pull request ever, so please go easy on me. :smile: "