Giter Club home page Giter Club logo

Comments (13)

mivade avatar mivade commented on June 30, 2024 19

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.

notdaniel avatar notdaniel commented on June 30, 2024 7

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.

tomchristie avatar tomchristie commented on June 30, 2024 6

Fair enough - I think on balance that it'd be reasonable to have this supported. Pull requests welcome.

from databases.

jimmy-lt avatar jimmy-lt commented on June 30, 2024 4

You do have server_default which is a bit more restrictive but still can be used as a potential workaround.

from databases.

dgilland avatar dgilland commented on June 30, 2024 3

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.

tomchristie avatar tomchristie commented on June 30, 2024 2

@euri10 Search in SQLAlchemy, not this project - it’s a table definitions thing.

from databases.

gvbgduh avatar gvbgduh commented on June 30, 2024 1

@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.

jordic avatar jordic commented on June 30, 2024 1

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.

tomchristie avatar tomchristie commented on June 30, 2024

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.

dgilland avatar dgilland commented on June 30, 2024

@gvbgduh Thanks for the suggestions! πŸ‘

from databases.

euri10 avatar euri10 commented on June 30, 2024

@gvbgduh what's the server_default you're referring to, I greped the source and found nothing?

from databases.

ryananguiano avatar ryananguiano commented on June 30, 2024

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.

WaldenWong avatar WaldenWong commented on June 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.