making bindparms required by default

13 views
Skip to first unread message

alex bodnaru

unread,
Aug 27, 2012, 12:06:19 PM8/27/12
to sqlalchemy

hello friends,

for a migrated system, i'm using textual queries in the form:

dbsession.execute("select name from table where id=:id", dict(id=1))

to my surprise, "select ... id=:idd" would still "work", asuming idd is null,
despite 'idd' is not in bindparms.

a 'required' argument to bindparam in _TextClause __init__() would indeed make
the 'idd' column required, thus raise an exception when not found in bind
dict(id=1).

is there an official way to acomplish this, or should i just hack in hardcoded?

thanks in advance,
alex

Michael Bayer

unread,
Aug 27, 2012, 12:49:10 PM8/27/12
to sqlal...@googlegroups.com
Here's a patch:

diff -r 594b0162e8a5 lib/sqlalchemy/sql/expression.py
--- a/lib/sqlalchemy/sql/expression.py Mon Aug 27 12:33:10 2012 -0400
+++ b/lib/sqlalchemy/sql/expression.py Mon Aug 27 12:43:48 2012 -0400
@@ -3110,7 +3110,7 @@
typemap[key] = sqltypes.to_instance(typemap[key])

def repl(m):
- self.bindparams[m.group(1)] = bindparam(m.group(1))
+ self.bindparams[m.group(1)] = bindparam(m.group(1), required=True)
return ':%s' % m.group(1)

# scan the string and search for bind parameter names, add them


however, this is too easy. There's lots of other places binds are generated. A rule such that if "value" is not passed to bindparam(), then flip the required flag on, would be the best behavior. needs a few lines in the bindparam() function, but also would need a lot of tests, in test/sql/test_query.py. ticket #2556 is added.

for your immediate needs, you can make a function that returns a Text() construct, and just supplies a dictionary of bindparam() objects to the Text() pre-established, with the required=True flag turned on.
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>

Michael Bayer

unread,
Aug 27, 2012, 3:13:31 PM8/27/12
to sqlal...@googlegroups.com
I'm super-hot to get the first betas of 0.8 out the door, and as this is a backwards-compatability-sensitive change, I was in a super rush to get this in, so it's in tip.

alex bodnaru

unread,
Aug 28, 2012, 2:59:48 AM8/28/12
to sqlal...@googlegroups.com

thanks michael,

On 08/27/2012 07:49 PM, Michael Bayer wrote:
> Here's a patch:
>
> diff -r 594b0162e8a5 lib/sqlalchemy/sql/expression.py
> --- a/lib/sqlalchemy/sql/expression.py Mon Aug 27 12:33:10 2012 -0400
> +++ b/lib/sqlalchemy/sql/expression.py Mon Aug 27 12:43:48 2012 -0400
> @@ -3110,7 +3110,7 @@
> typemap[key] = sqltypes.to_instance(typemap[key])
>
> def repl(m):
> - self.bindparams[m.group(1)] = bindparam(m.group(1))
> + self.bindparams[m.group(1)] = bindparam(m.group(1), required=True)
> return ':%s' % m.group(1)
>
> # scan the string and search for bind parameter names, add them
>
that's what i did indeed.
but it would break queries that are happy with the null on missing default.
thus, a configurable option, false by default, would do the job.
>
> however, this is too easy. There's lots of other places binds are generated. A rule such that if "value" is not passed to bindparam(), then flip the required flag on, would be the best behavior. needs a few lines in the bindparam() function, but also would need a lot of tests, in test/sql/test_query.py. ticket #2556 is added.
thank you very much. the patch is very sensible, but it will certainly break
scripts that rely on the null on missing behaviour.
however, i don't have such scripts ;).
>
> for your immediate needs, you can make a function that returns a Text() construct, and just supplies a dictionary of bindparam() objects to the Text() pre-established, with the required=True flag turned on.
>
thought of it too, but it won't do the work: i needed to catch :variables
missing in the given bindparm.
>
>
>
thanks a lot,
alex

alex bodnaru

unread,
Aug 28, 2012, 3:00:32 AM8/28/12
to sqlal...@googlegroups.com
On 08/27/2012 10:13 PM, Michael Bayer wrote:
> I'm super-hot to get the first betas of 0.8 out the door, and as this is a backwards-compatability-sensitive change, I was in a super rush to get this in, so it's in tip.
>
seen it' thanks again :).
Reply all
Reply to author
Forward
0 new messages