-
Notifications
You must be signed in to change notification settings - Fork 84
Better introspection integration with ActiveRecord::DB_DEFAULT? #121
Comments
Hmm. Good question. Summarizing your proposed change:
From a 'pure' sense this makes sense to me and I like it: if you don't specify anything at all you get the default; and if you want NULL then you specify My hesitations are:
Regarding that last point, we could get even more outlandish, but still sensible in its own way:
For columns that don't have a database default defined, the "default" value would be to insert NULL, same as always. But for columns that do have a a default database defined, if you want to override the default and insert a NULL you'd need to go out of your way to specify
Yeah, good idea. Regardless of what argument mechanism we use to tell rails to use the database default. After saving a model instance you'd expect the instance and the database record to be in sync. So that means if the database computed some column value it'd makes sense to propagate it back to the instance. |
My understanding of the Rails 3.2 behavior is that it supplies the default values when you instantiate a new model instance. For instance, run the following migration (using
Then create a new record:
Notice how it automatically supplies the default values for the columns with non-expression defaults. Furthermore, if you execute
My instinct is that this would be the desired behavior - we basically mimic as closely as possible what Rails already does for default values. We can't actually supply the default value because it needs to be calculated by the database when the record is saved, but we can do the next best thing. Then if someone explicitly assigns I suspect that fixing PostgreSQL (if I'm reading the code correctly) might be as simple as modifying
I'll try to get to it this weekend or early this week. I'll create a branch for all of this so we can evaluate when we want to pull it in. I agree that it does require a major version change since it is technically a backwards incompatibility, even though I think this new behavior is closer to what people expect. As such, we might want to wait until after @donv's work gets merged in. I'll also investigate approaches for keeping the model and the database in sync on a save. |
This is getting to be even more fun. Rails 4 appears to change the behavior of nils when it comes to inserting - if the The bad news is that since SQLite3 doesn't support DEFAULT (at least according to https://github.com/lomba/schema_plus#column-defaults-using), that means that the observed behavior of columns with expression defaults in SQLite3 changes from Rails 3 to Rails 4 - in Rails 3 you can't take advantage of the default, and in Rails 4 you can't avoid the default! All of this is making the test suite fun to write! |
I just pushed 42f1ea8 to https://github.com/lomba/schema_plus/tree/default_introspection. It has the I haven't attacked post-insert/update synchronization yet. |
yowza. amazing fact-finding mission you went on! i'm thinking the README should have a table or tables listing the behavior for:
not sure what the best way to format it would be... of course there'd be plenty of cells that would be N/A [if we were very super-clever, that table would be built from a json or xml data file, which we would also use to drive the specs. 1/2 😄 ] |
I experimented with
default: :now
today, and it appears that (at least in Rails 3.2 - I haven't tested Rails 4.0 yet) the developer must ensure that the defaulted attributes getActiveRecord::DB_DEFAULT
assigned to them, even during record creation.I'm wondering if it's desirable (and feasible) to hook into the ActiveRecord code that sets attributes to their default values during
new
so that it suppliesActiveRecord::DB_DEFAULT
for attributes for columns that have expression defaults.On a parallel note, when Rails inserts a new record with an auto-generated ID column, it uses
RETURNING
in the SQL to get the ID column value and updates the record to reflect that. I wonder if it's desirable (and feasible) to also have it return the values for the columns that usesActiveRecord::DB_DEFAULT
. In the absence of that I usedreload
, but it would reduce database round trips and improve the developer experience if it were handled automatically.Thoughts?
The text was updated successfully, but these errors were encountered: