Comments (13)
This would be really nice to have. Ignoring the default
value in the table definition currently makes this package not work as an async drop-in replacement for equivalent SQLAlchemy calls. For example:
url = "sqlite:///temp.db"
engine = sa.create_engine(url)
metadata = sa.MetaData()
table = sa.Table(
"table",
metadata,
sa.Column("Id", sa.Integer(), primary_key=True),
sa.Column("Thing", sa.Integer(), default=0, nullable=False),
)
metadata.create_all(bind=engine)
database = Database(url)
await database.connect()
await database.execute(table.insert(), values={"Id": 1})
results in IntegrityError: NOT NULL constraint failed: table.Thing
whereas the usual SQLAlchemy way
engine.execute(table.insert().values({"Id": 2}))
works as expected.
from databases.
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.
from databases.
Fair enough - I think on balance that it'd be reasonable to have this supported. Pull requests welcome.
from databases.
You do have server_default
which is a bit more restrictive but still can be used as a potential workaround.
from databases.
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 databases
.
from databases.
@euri10 Search in SQLAlchemy, not this project - itβs a table definitions thing.
from databases.
@dgilland you might consider server_default
which will apply the default at the DB level, like now()
func.
Another thing to consider at the app level is to delegate this to some validation schema/class/model.
As example, if data for insertion comes from the payload you might want to validate it before actually committing it to the db. One of many ways to do it might be a marshmallow
schema with some @post_load
action.
I am quite sure typesystem can do similar stuff as well.
from databases.
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:
https://github.com/vinissimus/asyncom/blob/master/asyncom/tests/test_default_values.py
from databases.
Good question.
Not necessarily, no. We might consider it out of scope. Itβs a bit of a blurry line, since everything else in the column data is all about the actual database schema, but default is an application-level indicator. We might choose not to support it here, but only do so at other layers of abstraction eg. The ORM package does have default support.
Not sure.
from databases.
@gvbgduh Thanks for the suggestions! π
from databases.
@gvbgduh what's the server_default
you're referring to, I greped the source and found nothing?
from databases.
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
from databases.
Using server_default
alone cannot completely solve this problem. You can try using the following approach.
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 thisdefault=datetime.now
, this method will be ineffective. Does the author have a better solution?
from databases.
Related Issues (20)
- Rotating Database Passwords
- Setting SSL Parameters in databases.Database() Method HOT 1
- Unable to retrieve foreignkey relation object from select statement HOT 1
- Password containing `/` cannot be used (MySQL)
- Support for UNIX domain socket for MySQL
- No module named 'asyncmy.connection' on python 3.11.3
- Warn for open transactions when a connection is garbage collected
- Unable to connect to database in docker
- MySQL Connection Pool Doesn't Seem to Work HOT 3
- Drop support for python 3.7 HOT 1
- New test `test_should_remove_ref_on_disconnect` in 0.8.0 fails on Fedora Linux HOT 10
- `IndexError` on force_rollback of Transaction in tests. HOT 13
- ImportError Postgresql HOT 1
- Transaction and gather HOT 2
- session manager/ session maker
- Allow passing string values for ssl query string in PostgreSQL URL HOT 1
- Version not match (0.8.0) depends on sqlalchemy (>=1.4.42,<1.5) HOT 2
- mypy's "has no attribute" error
- create table does not create unique keys and indexes
- new 0.8 version for sqlite connection with '?mode=ro' not working, Error can't open database
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from databases.