databases icon indicating copy to clipboard operation
databases copied to clipboard

Implement column defaults for INSERT/UPDATE

Open GarbageHamburger opened this issue 5 years ago • 10 comments

This PR implements usage of column defaults from SQLAlchemy. In regular SQLAlchemy the default column values are fetched using ExecutionContext._exec_default. However, we don't use the ExecutionContext here, so a function is implemented to take its place for column defaults.

Fixes #72

GarbageHamburger avatar May 04 '20 16:05 GarbageHamburger

Any updates on this?

GarbageHamburger avatar May 06 '20 18:05 GarbageHamburger

It's difficult because I'm not really sure if we want this functionality or not.

lovelydinosaur avatar May 07 '20 12:05 lovelydinosaur

@tomchristie I am just curious. Why would you not want this functionality? I am currently writing a application with fastapi and encode/databases and i want the timestamp to be created on insert. currently i don't have a way of doing this.

andre-dasilva avatar May 18 '20 09:05 andre-dasilva

It also seems necessary for UUID primary keys, which are pretty common these days.

camgunz avatar May 30 '20 18:05 camgunz

The column defaults are higher level sqla API ("Query") than the API used in databases ("Core"). Emulating them properly is quite hard. For example, the functions can access the context in several ways, and filling it is twisted.

The problem can be solved outside of databases package by adding a new method to your model base class to explicitly create defaults.

vmarkovtsev avatar Jun 02 '20 06:06 vmarkovtsev

@vmarkovtsev SQLAlchemy core does have defaults, see the default parameter for Column.__init__. I agree that correctly emulating defaults is hard due to the types of things a column default can be (it can be a ClauseElement!), but that's no reason for it to not be included.

GarbageHamburger avatar Jun 18 '20 12:06 GarbageHamburger

+1 for this. I followed the FastAPI tutorial and tried to implement a UUID for sqlite. I couldn't figure out why it didn't work until I specifically checked this repo for issues around the defaults. And defaults are indeed part of sqlalchemy core.

ricosuave73 avatar Jul 29 '20 13:07 ricosuave73

This is a must, thanks

elpapi42 avatar Aug 17 '20 22:08 elpapi42

Approve this pls

elpapi42 avatar Aug 24 '20 20:08 elpapi42

Any updates on this?

etscript avatar Aug 19 '22 08:08 etscript