volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Automatically add created_at / updated_at to whitelist #120

Closed westito closed 6 years ago

westito commented 7 years ago

When I use whitelist in update / insert method, I have to add created_at and updated_at to the white list every time. There is an option to add itt automatically?

aarondl commented 7 years ago

Hey westito. Sorry about the delay in response, I've been traveling for business abroad.

One of the problems that we have is that doing this prevents us from allowing the user to completely customize the whitelist. This breaks the intuitive thing about what a whitelist is. In the case where I want it updated all the time yes, it makes sense. But without adding too much extra complexity how do I tell the function to only update the field called "name" but NOT update created/updated at for whatever reasons I have at this time?

Also join us on Slack if you want to discuss in more realtime :) https://sqlboiler.from-the.cloud/

polderudo commented 6 years ago

Wouldn't it be nice to have some configuration where one can specify to always update the updated_at field? Only thing to take care of then would be to check if created_at is in the whitelist, and if not add it. Realy bad if one forgets to set the field manualy. Normaly if the field is present on the models, i think everyone want's to have it autoupdated, but for the once who don't want that, one could use the configuration setting.

aarondl commented 6 years ago

But then the question is, if they have this option on, how do they set ONLY name and NOT updated_at? It would force them to turn off the option, and fix all their code if they ever wanted this functionality.

Though I agree it's unfortunate, since it somewhat violates the principle of least surprise. Maybe we could document it more intensely.

polderudo commented 6 years ago

Why should one update only 'name' if the db has a field 'updated_at'. The meaning of updated_at IS to update the field every time something changes ... If one realy realy wants to not update updated_at, he can use raw-sql ...

aarondl commented 6 years ago

Okay, so I see three solutions to this problem and I'll probably pick one

Please vote for your favorite below:

polderudo commented 6 years ago

I'd vote for 1. Or 2, but not opt-in, but opt-out. Default would be that the update is done, and one can specify not to do the update.

aarondl commented 6 years ago

Will be opt-out and properly documented.

aarondl commented 6 years ago

This use case is handled by using boil.Greylist in v3. Greylist adds columns to the list of columns to insert, without completely the inference that sqlboiler normally does. This means created_at/updated_at will not be punted as soon as we want to specify a single column. I believe this resolves this.