GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Implement column defaults for INSERT/UPDATE by GarbageHamburger · Pull Request #206 · encode/databases
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Conversation
|
|
Any updates on this? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It's difficult because I'm not really sure if we want this functionality or not. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It also seems necessary for UUID primary keys, which are pretty common these days. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
The column defaults are higher level sqla API ("Query") than the API used in The problem can be solved outside of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@vmarkovtsev SQLAlchemy core does have defaults, see the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
+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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This is a must, thanks |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Approve this pls |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry fellows, I wish I approved this, but this looks too hacky right now to be included as a general solution.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
These are my suggestions 👍
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| # 2 paths where we apply column defaults: | ||
| # - values are supplied (the object must be a ValuesBase) | ||
| # - values is None but the object is a ValuesBase | ||
| if values is not None and not isinstance(query, ValuesBase): | ||
| raise TypeError("values supplied but query doesn't support .values()") | ||
|
|
||
| if values is not None or isinstance(query, ValuesBase): | ||
| values = Connection._apply_column_defaults(query, values) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| # 2 paths where we apply column defaults: | |
| # - values are supplied (the object must be a ValuesBase) | |
| # - values is None but the object is a ValuesBase | |
| if values is not None and not isinstance(query, ValuesBase): | |
| raise TypeError("values supplied but query doesn't support .values()") | |
| if values is not None or isinstance(query, ValuesBase): | |
| values = Connection._apply_column_defaults(query, values) | |
| elif values: | |
| assert isinstance(query, ValuesBase) | |
| values = self._apply_column_defaults(query, values) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if default.is_sequence: # pragma: no cover | ||
| # TODO: support sequences | ||
| continue |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
assert not default.is_sequence, "sequences are not supported, PRs welcome"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| values = values or {} | ||
|
|
||
| for column in query.table.c: | ||
| if column.name in values: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
values can have columns as keys, in such cases this will be always false
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| continue | ||
| elif default.is_callable: | ||
| value = default.arg(FakeExecutionContext()) | ||
| elif default.is_clause_element: # pragma: no cover |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| elif default.is_clause_element: # pragma: no cover | |
| assert not default.is_clause_element, "clause defaults are not supported, PRs welcome" |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| continue | ||
| else: | ||
| value = default.arg | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| blows up loudly and clearly. | ||
| """ | ||
|
|
||
| def __getattr__(self, _: str) -> typing.NoReturn: # pragma: no cover |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We should cover this by another test which tests raising NotImplementedError
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return str(self) == str(other) | ||
|
|
||
|
|
||
| class FakeExecutionContext: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| class FakeExecutionContext: | |
| class DummyExecutionContext: |
(it's not completely fake, after all)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| """ | ||
|
|
||
| def __getattr__(self, _: str) -> typing.NoReturn: # pragma: no cover | ||
| raise NotImplementedError( |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
btw: my own custom implementation of this (yep, I have a similar hack in my prod), I pass the current values to this context so that it becomes much more useful.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Any updates on this? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
At least 1 approving review is required to merge this pull request.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Support for "default" parameter in sqlalchemy.Column
There was an error while loading. Please reload this page.