This project is archived and is in readonly mode.
create and find_or_create_by behaves differently on model with no :id column
-
Alexey Nayden
- Tag changed from :id primary_key to :id primary_key, 2.3.5, active_record
-
Alexey Nayden
- Tag changed from :id primary_key, 2.3.5, active_record to :id primary_key, 2.3.5, active_record, invalid
@Horace actually AR always marks primary key column as attr_protected, so you're unable to mass-assign it with hash in #create. If you try to activate logging to stdout ActiveRecord::Base.logger = Logger.new(STDOUT) you'll get an error message regarding mass-assignment protected attribute. So in your case you're trying to create a record without PK column set to any value. And due to your database inconsistency in that example (i.e. PK insn't NOT NULL and UNIQUE as it always should), DB allows you to create such invalid record. If you set DB constraints for PK, you'd get an error — all correct.
That's it.
-
Alexey Nayden
And, of course, your example is obviously fixed with "c = Country.new; c.code = 'X2'; c.save"