-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for "default" parameter in sqlalchemy.Column #72
Comments
Good question. Not sure. |
For me personally, I'm not interested in using an ORM so it would be nice if application-level defaults were supported. However, I completely understand if you think that's something out-of-scope for |
@dgilland you might consider |
@gvbgduh Thanks for the suggestions! 👍 |
@gvbgduh what's the |
@euri10 Search in SQLAlchemy, not this project - it’s a table definitions thing. |
You can generate the defaults yourself like this: def get_defaults(db_table):
defaults = {}
for column in db_table.columns:
if column.default is not None:
value = column.default.arg
if callable(value):
value = value()
defaults[column.name] = value
return defaults |
In my latest relase of asyncom I support this case for simple cases (values and callables.) there are also other defaults supported. https://github.com/vinissimus/asyncom/blob/master/asyncom/om.py#L167 Here the tests: |
This would be really nice to have. Ignoring the
results in
works as expected. |
I have to agree, this feels to me like it's within the scope of the project and currently rather unexpected behavior. If something supports and recommends SQLAlchemy table definitions, it's pretty confusing if it supports most options but a few are just left out. Workarounds above are fine and of course there are many ways we can abstract this out ourselves pretty easily/quickly, so it's not the end of the world if you truly feel supporting this would be outside the scope of this project. But in that case, it would be great if you could specify in the documentation which features are not supported. Simple as the solution is, this is only useful once the user has become aware of the problem. Only discovering limitations when a row insert yields an unexpected result isn't terribly friendly, and makes me wonder if anything else is quietly unsupported that I'll only become aware of down the line if/when I try to use it. |
Fair enough - I think on balance that it'd be reasonable to have this supported. Pull requests welcome. |
You do have |
Using table = sa.Table(
"table",
metadata,
sa.Column("Id", sa.Integer(), primary_key=True),
sa.Column("Thing", sa.Integer(), default=sa.cast(0, sa.Integer), nullable=False),
) However, if the default value is set as a function body to generate the default value in real-time when adding data,like this |
For me the issue was caused by the fact that the default value should be specified in quotes e.g.
|
this work for me. Thanx! |
Whenever I have SQLAlchemy table column with a default value (e.g.
Column("verified", Boolean(), default=False)
), the resulting insert statement generated bydatabases
sets anull
value for the column when that column isn't provided (e.g.db.execute(mytable.insert(), {...})
where "verified" isn't set).Are there plans to support column defaults in insert statements?
The text was updated successfully, but these errors were encountered: