[sqlalchemy] Getting default value from python object
I understand default value is used to emit Insert statement. Is there any way to have default value is accessible via python object. See below example >>> class User(Base): ... __tablename__ = 'users' ... id = Column(Integer, primary_key=True) ... name = Column(String) ... password = Column(String, default='welcome') ... >>> ed_user = User(name='ed') >>> print(ed_user.name) ed >>> print(ed_user.password) None Expect `print(ed_user.password)` to return 'welcome' in this case. Adding it to a session, flush and query the object will return default value but is there a way to get default value without re-querying the object? -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] mapper existance checks possibly wrong
On Wed, Oct 25, 2017 at 9:25 AM, Антонио Антуан wrote: > As I said befire, sqlalchemy version: 1.0.19 and code is here: > https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f I apologize, did not see that link. Use the execute_and_instances() implemenation from lib/sqlalchemy/orm/query.py, which means here use _bind_mapper(): def _execute_and_instances(self, querycontext): self._check_bound_to_shard() querycontext.attributes['shard_id'] = self._shard_id result = self._connection_from_session( mapper=self._bind_mapper(), shard_id=self._shard_id).execute( querycontext.statement, self._params ) return self.instances(result, querycontext) > > Currently I can't use another version. In that case, I think that here is > the right decision: > "Looks like I have to check if "_mapper_zero()" returns real mapper. > Otherwise I should pass "None" to "_connection_from_session()" as value of > "mapper" argument" > > Am I right? > > > ср, 25 окт. 2017 г. в 16:18, Mike Bayer : >> >> On Wed, Oct 25, 2017 at 6:52 AM, Антонио Антуан >> wrote: >> > Any news here? >> >> We would require a self-contained demonstration case that illustrates >> your error as well as complete information on what specific SQLAlchemy >> version you are targeting. As mentioned in a different reply, >> _mapper_zero has been fixed in 1.1 to always return a mapper. I >> would strongly advise targeting the 1.1 series for any new development >> as that's where issues can still be fixed. >> >> >> >> >> >> > >> > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан >> > написал: >> >> >> >> I see that it is not happened when "bind" passed directly to >> >> "sessionmaker" >> >> >> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >> >>> >> >>> >> >>> >> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >> >>> написал: >> >> On Fri, Oct 20, 2017 at 11:05 AM, Simon King >> wrote: >> > The "is not None" is important when checking a variable that may >> > contain a ClauseElement, precisely because ClauseElement defines >> > that >> > __bool__ method. >> > >> > However, in Session.get_bind(), "mapper" is not supposed to contain >> > a >> > ClauseElement. It should either be an instance of >> > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" >> > is a >> > valid and concise way to write it. Comparing to None might be >> > strictly >> > more accurate, but it shouldn't be necessary. >> >> agree, "mapper" means "mapper" and it is not supposed to be a >> ClauseElement. This sounds like arguments are not being passed >> correctly somewhere. Would need a demonstration script if something >> in SQLAlchemy itself is claimed to be making the mistake. >> >>> >> >>> >> >>> Ok, that code produces mentioned error: >> >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >> >>> Traceback (most recent call last): >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >> >>> >> >>> Session.query(with_recursive).set_shard('default').all() >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> >>> 2654, >> >>> in all >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> >>> 2802, >> >>> in __iter__ >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >> >>> _execute_and_instances >> >>> shard_id=self._shard_id).execute( >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> >>> 2806, >> >>> in _connection_from_session >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> >>> 984, in connection >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >> >>> get_bind >> >>> original_bind = super(RoutingSession, self).get_bind(mapper, >> >>> clause) >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> >>> 1336, in get_bind >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line >> >>> 539, in __bool__ >> >>> TypeError: Boolean value of this clause is not defined >> >>> >> >> > >> > Simon >> >> > >> > >> > >> > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан >> > wrote: >> >> Really, `mapper` contains this: > >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >> >> already >> >> encountered this problem in my code, when I checked sqla-objects >> >> existance >> >> without "is not None", project was broken. Is it normal to omit >> >> that >> >> condition in sqlalchemy code? >> >> >> >> I use: >> > sqlalchemy.__version__ >> >> '1.0.19' >> >> >> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon >> >> King >> >> написал: >> >>
Re: [sqlalchemy] mapper existance checks possibly wrong
As I said befire, sqlalchemy version: 1.0.19 and code is here: https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f Currently I can't use another version. In that case, I think that here is the right decision: "Looks like I have to check if "_mapper_zero()" returns real mapper. Otherwise I should pass "None" to "_connection_from_session()" as value of "mapper" argument" Am I right? ср, 25 окт. 2017 г. в 16:18, Mike Bayer : > On Wed, Oct 25, 2017 at 6:52 AM, Антонио Антуан > wrote: > > Any news here? > > We would require a self-contained demonstration case that illustrates > your error as well as complete information on what specific SQLAlchemy > version you are targeting. As mentioned in a different reply, > _mapper_zero has been fixed in 1.1 to always return a mapper. I > would strongly advise targeting the 1.1 series for any new development > as that's where issues can still be fixed. > > > > > > > > > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан > > написал: > >> > >> I see that it is not happened when "bind" passed directly to > >> "sessionmaker" > >> > >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : > >>> > >>> > >>> > >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer > >>> написал: > > On Fri, Oct 20, 2017 at 11:05 AM, Simon King > wrote: > > The "is not None" is important when checking a variable that may > > contain a ClauseElement, precisely because ClauseElement defines > that > > __bool__ method. > > > > However, in Session.get_bind(), "mapper" is not supposed to contain > a > > ClauseElement. It should either be an instance of > > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" > is a > > valid and concise way to write it. Comparing to None might be > strictly > > more accurate, but it shouldn't be necessary. > > agree, "mapper" means "mapper" and it is not supposed to be a > ClauseElement. This sounds like arguments are not being passed > correctly somewhere. Would need a demonstration script if something > in SQLAlchemy itself is claimed to be making the mistake. > >>> > >>> > >>> Ok, that code produces mentioned error: > >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f > >>> Traceback (most recent call last): > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in > >>> > >>> Session.query(with_recursive).set_shard('default').all() > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2654, > >>> in all > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2802, > >>> in __iter__ > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in > >>> _execute_and_instances > >>> shard_id=self._shard_id).execute( > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2806, > >>> in _connection_from_session > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line > >>> 984, in connection > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in > >>> get_bind > >>> original_bind = super(RoutingSession, self).get_bind(mapper, > clause) > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line > >>> 1336, in get_bind > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line > >>> 539, in __bool__ > >>> TypeError: Boolean value of this clause is not defined > >>> > > > > > Simon > > > > > > > > > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан > > wrote: > >> Really, `mapper` contains this: >> 0x7fe673fb0a50; group_getter>. But does it changes something? I > >> already > >> encountered this problem in my code, when I checked sqla-objects > >> existance > >> without "is not None", project was broken. Is it normal to omit > that > >> condition in sqlalchemy code? > >> > >> I use: > > sqlalchemy.__version__ > >> '1.0.19' > >> > >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon King > >> написал: > >>> > >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан < > a.ch...@gmail.com> > >>> wrote: > >>> > Hi. > >>> > I use my own `RoutingSession` and `RoutingQuery` implementation, > >>> > most of > >>> > it > >>> > inspired by `sqlalchemy.ext.horizontal_shard`: > >>> > > >>> > class RoutingSession(Session): > >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, > >>> > **kwargs): > >>> > original_bind = None > >>> > try: > >>> > original_bind = super(RoutingSession, > >>> > self).get_bind(mapper, > >>> > clause) > >>> > except UnboundExecutionError: > >>> > # may not be bound > >>> > pass > >>> > if shard_id is None: > >>
Re: [sqlalchemy] mapper existance checks possibly wrong
On Wed, Oct 25, 2017 at 6:52 AM, Антонио Антуан wrote: > Any news here? We would require a self-contained demonstration case that illustrates your error as well as complete information on what specific SQLAlchemy version you are targeting. As mentioned in a different reply, _mapper_zero has been fixed in 1.1 to always return a mapper. I would strongly advise targeting the 1.1 series for any new development as that's where issues can still be fixed. > > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан > написал: >> >> I see that it is not happened when "bind" passed directly to >> "sessionmaker" >> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >>> >>> >>> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >>> написал: On Fri, Oct 20, 2017 at 11:05 AM, Simon King wrote: > The "is not None" is important when checking a variable that may > contain a ClauseElement, precisely because ClauseElement defines that > __bool__ method. > > However, in Session.get_bind(), "mapper" is not supposed to contain a > ClauseElement. It should either be an instance of > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" is a > valid and concise way to write it. Comparing to None might be strictly > more accurate, but it shouldn't be necessary. agree, "mapper" means "mapper" and it is not supposed to be a ClauseElement. This sounds like arguments are not being passed correctly somewhere. Would need a demonstration script if something in SQLAlchemy itself is claimed to be making the mistake. >>> >>> >>> Ok, that code produces mentioned error: >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >>> Traceback (most recent call last): >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >>> >>> Session.query(with_recursive).set_shard('default').all() >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2654, >>> in all >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2802, >>> in __iter__ >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >>> _execute_and_instances >>> shard_id=self._shard_id).execute( >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2806, >>> in _connection_from_session >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> 984, in connection >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >>> get_bind >>> original_bind = super(RoutingSession, self).get_bind(mapper, clause) >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> 1336, in get_bind >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line >>> 539, in __bool__ >>> TypeError: Boolean value of this clause is not defined >>> > > Simon > > > > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан > wrote: >> Really, `mapper` contains this: >>> >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >> already >> encountered this problem in my code, when I checked sqla-objects >> existance >> without "is not None", project was broken. Is it normal to omit that >> condition in sqlalchemy code? >> >> I use: > sqlalchemy.__version__ >> '1.0.19' >> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon King >> написал: >>> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан >>> wrote: >>> > Hi. >>> > I use my own `RoutingSession` and `RoutingQuery` implementation, >>> > most of >>> > it >>> > inspired by `sqlalchemy.ext.horizontal_shard`: >>> > >>> > class RoutingSession(Session): >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, >>> > **kwargs): >>> > original_bind = None >>> > try: >>> > original_bind = super(RoutingSession, >>> > self).get_bind(mapper, >>> > clause) >>> > except UnboundExecutionError: >>> > # may not be bound >>> > pass >>> > if shard_id is None: >>> > shard_id = TenantIDStorage.get_shard_id() # just >>> > global >>> > storage >>> > bind_for_shard = self.__binds[shard_id] >>> > if original_bind is not None and original_bind.url == >>> > bind_for_shard.url: >>> > return original_bind >>> > else: >>> > return bind_for_shard >>> > >>> > def __init__(self, shards=None, query_cls=CachingQuery, >>> > engines_factory=None, **kwargs): >>> > super(RoutingSession, self).__init__(query_cls=query_cls, >>> > **kwargs) >>> > self.__binds = {} >>> > self.engines_factory = engines_factory >>> >
Re: [sqlalchemy] mapper existance checks possibly wrong
On Wed, Oct 25, 2017 at 8:02 AM, Антонио Антуан wrote: > Looks like I have to check if _mapper_zero() returns real mapper. _mapper_zero() in the 1.1 and 1.2 series will never return a non-mapper (see https://bitbucket.org/zzzeek/sqlalchemy/issues/3608). I would strongly advise working with the 1.1. series at least as 1.0.x. is security patches only. Otherwise > I should pass None to "_connection_from_session()" as value of "mapper" > argument. Right? > > ср, 25 окт. 2017 г. в 15:00, Антонио Антуан : >> >> As I mentioned before, "> group_getter>". >> >> ср, 25 окт. 2017 г. в 14:19, Simon King : >>> >>> What does self._mapper_zero() return in your >>> RoutingQuery._execute_and_instances method? >>> >>> Simon >>> >>> On Wed, Oct 25, 2017 at 11:52 AM, Антонио Антуан >>> wrote: >>> > Any news here? >>> > >>> > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан >>> > написал: >>> >> >>> >> I see that it is not happened when "bind" passed directly to >>> >> "sessionmaker" >>> >> >>> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >>> >>> >>> >>> >>> >>> >>> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >>> >>> написал: >>> >>> On Fri, Oct 20, 2017 at 11:05 AM, Simon King >>> >>> wrote: >>> > The "is not None" is important when checking a variable that may >>> > contain a ClauseElement, precisely because ClauseElement defines >>> > that >>> > __bool__ method. >>> > >>> > However, in Session.get_bind(), "mapper" is not supposed to >>> > contain a >>> > ClauseElement. It should either be an instance of >>> > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" >>> > is a >>> > valid and concise way to write it. Comparing to None might be >>> > strictly >>> > more accurate, but it shouldn't be necessary. >>> >>> agree, "mapper" means "mapper" and it is not supposed to be a >>> ClauseElement. This sounds like arguments are not being passed >>> correctly somewhere. Would need a demonstration script if >>> something >>> in SQLAlchemy itself is claimed to be making the mistake. >>> >>> >>> >>> >>> >>> Ok, that code produces mentioned error: >>> >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >>> >>> Traceback (most recent call last): >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >>> >>> >>> >>> Session.query(with_recursive).set_shard('default').all() >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2654, >>> >>> in all >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2802, >>> >>> in __iter__ >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >>> >>> _execute_and_instances >>> >>> shard_id=self._shard_id).execute( >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2806, >>> >>> in _connection_from_session >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> >>> 984, in connection >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >>> >>> get_bind >>> >>> original_bind = super(RoutingSession, self).get_bind(mapper, >>> >>> clause) >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> >>> 1336, in get_bind >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", >>> >>> line >>> >>> 539, in __bool__ >>> >>> TypeError: Boolean value of this clause is not defined >>> >>> >>> >>> > >>> > Simon >>> >>> > >>> > >>> > >>> > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан >>> > >>> > wrote: >>> >> Really, `mapper` contains this: >> >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >>> >> already >>> >> encountered this problem in my code, when I checked sqla-objects >>> >> existance >>> >> without "is not None", project was broken. Is it normal to omit >>> >> that >>> >> condition in sqlalchemy code? >>> >> >>> >> I use: >>> > sqlalchemy.__version__ >>> >> '1.0.19' >>> >> >>> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon >>> >> King >>> >> написал: >>> >>> >>> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан >>> >>> >>> >>> wrote: >>> >>> > Hi. >>> >>> > I use my own `RoutingSession` and `RoutingQuery` >>> >>> > implementation, >>> >>> > most of >>> >>> > it >>> >>> > inspired by `sqlalchemy.ext.horizontal_shard`: >>> >>> > >>> >>> > class RoutingSession(Session): >>> >>> > def get_bind(self, mapper=None, clause=None, >>> >>> > shard_id=None, >>> >>> > **kwargs): >>> >>> > original_bind = None >>> >>> > try: >>> >>> > original_bind = super(RoutingSession, >>> >>> > self
Re: [sqlalchemy] sqlalchemy.select().group_by(expr) doesn't use label of expression, while .order_by(expr) does
On Wed, Oct 25, 2017 at 8:55 AM, Gijs Molenaar wrote: > > > Op vrijdag 20 oktober 2017 19:11:56 UTC+2 schreef Mike Bayer: >> >> On Thu, Oct 19, 2017 at 4:25 AM, Gijs Molenaar >> wrote: >> > >> > >> > Op donderdag 19 oktober 2017 03:10:21 UTC+2 schreef Mike Bayer: >> >> >> >> On Wed, Oct 18, 2017 at 7:38 AM, Gijs Molenaar >> >> wrote: >> >> > hi! >> >> > >> >> > >> >> > Not sure if this a bug or something I should in my SQLAlchemy >> >> > dialect, >> >> > but >> >> > currently >> >> > >> >> > >> >> > expr = (table.c.x + table.c.y).label('lx') >> >> > select([func.count(table.c.id), expr]).group_by(expr).order_by(expr) >> >> > >> >> > compiles to: >> >> > >> >> > SELECT count(some_table.id) AS count_1, some_table.x + some_table.y >> >> > AS >> >> > lx >> >> > \nFROM some_table GROUP BY some_table.x + some_table.y ORDER BY lx; >> >> > >> >> > >> >> > which works fine for for example sqlite, but MonetDB requires the use >> >> > of >> >> > the >> >> > lx label in the GROUP BY, which I think makes sense? Should this be >> >> > addressed on the SQLalchemy side or on the MonetDB dialect side? >> >> >> >> so this was overhauled in >> >> >> >> >> >> http://docs.sqlalchemy.org/en/latest/changelog/migration_09.html#label-constructs-can-now-render-as-their-name-alone-in-an-order-by >> >> where we changed ORDER BY to use the label name when the expression is >> >> passed. >> >> >> >> so the immediate answer would be to not actually order by the label(). >> > >> > >> > I think i didn't formulate my e-mail correctly. The ORDER BY is not the >> > problem, it is the GROUP BY that requires a label. If the GROUP BY >> > doesn't >> > use a label (just like ORDER BY), MonetDB doesn't want to eat the query. >> >> this is my fault because I read/reply to these emails very fast and >> focus mainly on the code I see.If you want it the other way >> around, there is the feature described at: >> >> >> https://docs.sqlalchemy.org/en/latest/changelog/migration_10.html#order-by-and-group-by-are-special-cases >> >> that is, say group_by("somelabel"). >> >> If you need this to be more automatic for this backend, we can look >> into seeing how the "ORDER BY" version of the feature can be more >> generalized on a backend-specific basis but this would be longer-term. >> > > Hi Mike, > > My main concern is to get the test suite pass for now :) What you propose > doesn't make the dialect work with the current test suite right? I assume this is the single test test_group_by_composed, for the immediate case you can override it in your local test_suite.py file. We can add an @exclusion for it too. > > greetings, > > - Gijs > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] sqlalchemy.select().group_by(expr) doesn't use label of expression, while .order_by(expr) does
Op vrijdag 20 oktober 2017 19:11:56 UTC+2 schreef Mike Bayer: > > On Thu, Oct 19, 2017 at 4:25 AM, Gijs Molenaar > wrote: > > > > > > Op donderdag 19 oktober 2017 03:10:21 UTC+2 schreef Mike Bayer: > >> > >> On Wed, Oct 18, 2017 at 7:38 AM, Gijs Molenaar > >> wrote: > >> > hi! > >> > > >> > > >> > Not sure if this a bug or something I should in my SQLAlchemy > dialect, > >> > but > >> > currently > >> > > >> > > >> > expr = (table.c.x + table.c.y).label('lx') > >> > select([func.count(table.c.id), expr]).group_by(expr).order_by(expr) > >> > > >> > compiles to: > >> > > >> > SELECT count(some_table.id) AS count_1, some_table.x + some_table.y > AS > >> > lx > >> > \nFROM some_table GROUP BY some_table.x + some_table.y ORDER BY lx; > >> > > >> > > >> > which works fine for for example sqlite, but MonetDB requires the use > of > >> > the > >> > lx label in the GROUP BY, which I think makes sense? Should this be > >> > addressed on the SQLalchemy side or on the MonetDB dialect side? > >> > >> so this was overhauled in > >> > >> > http://docs.sqlalchemy.org/en/latest/changelog/migration_09.html#label-constructs-can-now-render-as-their-name-alone-in-an-order-by > > >> where we changed ORDER BY to use the label name when the expression is > >> passed. > >> > >> so the immediate answer would be to not actually order by the label(). > > > > > > I think i didn't formulate my e-mail correctly. The ORDER BY is not the > > problem, it is the GROUP BY that requires a label. If the GROUP BY > doesn't > > use a label (just like ORDER BY), MonetDB doesn't want to eat the query. > > this is my fault because I read/reply to these emails very fast and > focus mainly on the code I see.If you want it the other way > around, there is the feature described at: > > > https://docs.sqlalchemy.org/en/latest/changelog/migration_10.html#order-by-and-group-by-are-special-cases > > > that is, say group_by("somelabel"). > > If you need this to be more automatic for this backend, we can look > into seeing how the "ORDER BY" version of the feature can be more > generalized on a backend-specific basis but this would be longer-term. > > Hi Mike, My main concern is to get the test suite pass for now :) What you propose doesn't make the dialect work with the current test suite right? greetings, - Gijs -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] mapper existance checks possibly wrong
This is complete guesswork - I haven't examined this code in detail so I could be wrong. Also, I don't know what version of SQLAlchemy you are using, so this may not apply. Session.connection takes separate "mapper" and "clause" parameters: https://bitbucket.org/zzzeek/sqlalchemy/src/f34b180ca9059a74c3bf1db1b79e187c3f4b81c9/lib/sqlalchemy/orm/session.py#session.py-958 Query._execute_and_instances passes both of those parameters: https://bitbucket.org/zzzeek/sqlalchemy/src/f34b180ca9059a74c3bf1db1b79e187c3f4b81c9/lib/sqlalchemy/orm/query.py#query.py-2893 (via the _get_bind_args helper function) Your overridden version of _execute_and_instances is passing a clause in the "mapper" parameter, which seems wrong. Somehow you need to fix that, perhaps by using the _get_bind_args helper. Simon On Wed, Oct 25, 2017 at 1:02 PM, Антонио Антуан wrote: > Looks like I have to check if _mapper_zero() returns real mapper. Otherwise > I should pass None to "_connection_from_session()" as value of "mapper" > argument. Right? > > ср, 25 окт. 2017 г. в 15:00, Антонио Антуан : >> >> As I mentioned before, "> group_getter>". >> >> ср, 25 окт. 2017 г. в 14:19, Simon King : >>> >>> What does self._mapper_zero() return in your >>> RoutingQuery._execute_and_instances method? >>> >>> Simon >>> >>> On Wed, Oct 25, 2017 at 11:52 AM, Антонио Антуан >>> wrote: >>> > Any news here? >>> > >>> > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан >>> > написал: >>> >> >>> >> I see that it is not happened when "bind" passed directly to >>> >> "sessionmaker" >>> >> >>> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >>> >>> >>> >>> >>> >>> >>> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >>> >>> написал: >>> >>> On Fri, Oct 20, 2017 at 11:05 AM, Simon King >>> >>> wrote: >>> > The "is not None" is important when checking a variable that may >>> > contain a ClauseElement, precisely because ClauseElement defines >>> > that >>> > __bool__ method. >>> > >>> > However, in Session.get_bind(), "mapper" is not supposed to >>> > contain a >>> > ClauseElement. It should either be an instance of >>> > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" >>> > is a >>> > valid and concise way to write it. Comparing to None might be >>> > strictly >>> > more accurate, but it shouldn't be necessary. >>> >>> agree, "mapper" means "mapper" and it is not supposed to be a >>> ClauseElement. This sounds like arguments are not being passed >>> correctly somewhere. Would need a demonstration script if >>> something >>> in SQLAlchemy itself is claimed to be making the mistake. >>> >>> >>> >>> >>> >>> Ok, that code produces mentioned error: >>> >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >>> >>> Traceback (most recent call last): >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >>> >>> >>> >>> Session.query(with_recursive).set_shard('default').all() >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2654, >>> >>> in all >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2802, >>> >>> in __iter__ >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >>> >>> _execute_and_instances >>> >>> shard_id=self._shard_id).execute( >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >>> >>> 2806, >>> >>> in _connection_from_session >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> >>> 984, in connection >>> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >>> >>> get_bind >>> >>> original_bind = super(RoutingSession, self).get_bind(mapper, >>> >>> clause) >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> >>> 1336, in get_bind >>> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", >>> >>> line >>> >>> 539, in __bool__ >>> >>> TypeError: Boolean value of this clause is not defined >>> >>> >>> >>> > >>> > Simon >>> >>> > >>> > >>> > >>> > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан >>> > >>> > wrote: >>> >> Really, `mapper` contains this: >> >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >>> >> already >>> >> encountered this problem in my code, when I checked sqla-objects >>> >> existance >>> >> without "is not None", project was broken. Is it normal to omit >>> >> that >>> >> condition in sqlalchemy code? >>> >> >>> >> I use: >>> > sqlalchemy.__version__ >>> >> '1.0.19' >>> >> >>> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon >>> >> King >>> >> написал: >>> >>> >>> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан >>> >>> >>
Re: [sqlalchemy] mapper existance checks possibly wrong
Looks like I have to check if _mapper_zero() returns real mapper. Otherwise I should pass None to "_connection_from_session()" as value of "mapper" argument. Right? ср, 25 окт. 2017 г. в 15:00, Антонио Антуан : > As I mentioned before, " group_getter>". > > ср, 25 окт. 2017 г. в 14:19, Simon King : > >> What does self._mapper_zero() return in your >> RoutingQuery._execute_and_instances method? >> >> Simon >> >> On Wed, Oct 25, 2017 at 11:52 AM, Антонио Антуан >> wrote: >> > Any news here? >> > >> > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан >> > написал: >> >> >> >> I see that it is not happened when "bind" passed directly to >> >> "sessionmaker" >> >> >> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >> >>> >> >>> >> >>> >> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >> >>> написал: >> >> On Fri, Oct 20, 2017 at 11:05 AM, Simon King > > >> wrote: >> > The "is not None" is important when checking a variable that may >> > contain a ClauseElement, precisely because ClauseElement defines >> that >> > __bool__ method. >> > >> > However, in Session.get_bind(), "mapper" is not supposed to >> contain a >> > ClauseElement. It should either be an instance of >> > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" >> is a >> > valid and concise way to write it. Comparing to None might be >> strictly >> > more accurate, but it shouldn't be necessary. >> >> agree, "mapper" means "mapper" and it is not supposed to be a >> ClauseElement. This sounds like arguments are not being passed >> correctly somewhere. Would need a demonstration script if something >> in SQLAlchemy itself is claimed to be making the mistake. >> >>> >> >>> >> >>> Ok, that code produces mentioned error: >> >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >> >>> Traceback (most recent call last): >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >> >>> >> >>> Session.query(with_recursive).set_shard('default').all() >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> 2654, >> >>> in all >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> 2802, >> >>> in __iter__ >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >> >>> _execute_and_instances >> >>> shard_id=self._shard_id).execute( >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line >> 2806, >> >>> in _connection_from_session >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> >>> 984, in connection >> >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >> >>> get_bind >> >>> original_bind = super(RoutingSession, self).get_bind(mapper, >> clause) >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> >>> 1336, in get_bind >> >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line >> >>> 539, in __bool__ >> >>> TypeError: Boolean value of this clause is not defined >> >>> >> >> > >> > Simon >> >> > >> > >> > >> > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан > > >> > wrote: >> >> Really, `mapper` contains this: > >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >> >> already >> >> encountered this problem in my code, when I checked sqla-objects >> >> existance >> >> without "is not None", project was broken. Is it normal to omit >> that >> >> condition in sqlalchemy code? >> >> >> >> I use: >> > sqlalchemy.__version__ >> >> '1.0.19' >> >> >> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon >> King >> >> написал: >> >>> >> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан < >> a.ch...@gmail.com> >> >>> wrote: >> >>> > Hi. >> >>> > I use my own `RoutingSession` and `RoutingQuery` >> implementation, >> >>> > most of >> >>> > it >> >>> > inspired by `sqlalchemy.ext.horizontal_shard`: >> >>> > >> >>> > class RoutingSession(Session): >> >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, >> >>> > **kwargs): >> >>> > original_bind = None >> >>> > try: >> >>> > original_bind = super(RoutingSession, >> >>> > self).get_bind(mapper, >> >>> > clause) >> >>> > except UnboundExecutionError: >> >>> > # may not be bound >> >>> > pass >> >>> > if shard_id is None: >> >>> > shard_id = TenantIDStorage.get_shard_id() # just >> >>> > global >> >>> > storage >> >>> > bind_for_shard = self.__binds[shard_id] >> >>> > if original_bind is not None and original_bind.url == >> >>> > bind_for_shard.url: >> >>> > re
Re: [sqlalchemy] mapper existance checks possibly wrong
As I mentioned before, "". ср, 25 окт. 2017 г. в 14:19, Simon King : > What does self._mapper_zero() return in your > RoutingQuery._execute_and_instances method? > > Simon > > On Wed, Oct 25, 2017 at 11:52 AM, Антонио Антуан > wrote: > > Any news here? > > > > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан > > написал: > >> > >> I see that it is not happened when "bind" passed directly to > >> "sessionmaker" > >> > >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : > >>> > >>> > >>> > >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer > >>> написал: > > On Fri, Oct 20, 2017 at 11:05 AM, Simon King > wrote: > > The "is not None" is important when checking a variable that may > > contain a ClauseElement, precisely because ClauseElement defines > that > > __bool__ method. > > > > However, in Session.get_bind(), "mapper" is not supposed to contain > a > > ClauseElement. It should either be an instance of > > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" > is a > > valid and concise way to write it. Comparing to None might be > strictly > > more accurate, but it shouldn't be necessary. > > agree, "mapper" means "mapper" and it is not supposed to be a > ClauseElement. This sounds like arguments are not being passed > correctly somewhere. Would need a demonstration script if something > in SQLAlchemy itself is claimed to be making the mistake. > >>> > >>> > >>> Ok, that code produces mentioned error: > >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f > >>> Traceback (most recent call last): > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in > >>> > >>> Session.query(with_recursive).set_shard('default').all() > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2654, > >>> in all > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2802, > >>> in __iter__ > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in > >>> _execute_and_instances > >>> shard_id=self._shard_id).execute( > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line > 2806, > >>> in _connection_from_session > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line > >>> 984, in connection > >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in > >>> get_bind > >>> original_bind = super(RoutingSession, self).get_bind(mapper, > clause) > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line > >>> 1336, in get_bind > >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line > >>> 539, in __bool__ > >>> TypeError: Boolean value of this clause is not defined > >>> > > > > > Simon > > > > > > > > > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан > > wrote: > >> Really, `mapper` contains this: >> 0x7fe673fb0a50; group_getter>. But does it changes something? I > >> already > >> encountered this problem in my code, when I checked sqla-objects > >> existance > >> without "is not None", project was broken. Is it normal to omit > that > >> condition in sqlalchemy code? > >> > >> I use: > > sqlalchemy.__version__ > >> '1.0.19' > >> > >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon King > >> написал: > >>> > >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан < > a.ch...@gmail.com> > >>> wrote: > >>> > Hi. > >>> > I use my own `RoutingSession` and `RoutingQuery` implementation, > >>> > most of > >>> > it > >>> > inspired by `sqlalchemy.ext.horizontal_shard`: > >>> > > >>> > class RoutingSession(Session): > >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, > >>> > **kwargs): > >>> > original_bind = None > >>> > try: > >>> > original_bind = super(RoutingSession, > >>> > self).get_bind(mapper, > >>> > clause) > >>> > except UnboundExecutionError: > >>> > # may not be bound > >>> > pass > >>> > if shard_id is None: > >>> > shard_id = TenantIDStorage.get_shard_id() # just > >>> > global > >>> > storage > >>> > bind_for_shard = self.__binds[shard_id] > >>> > if original_bind is not None and original_bind.url == > >>> > bind_for_shard.url: > >>> > return original_bind > >>> > else: > >>> > return bind_for_shard > >>> > > >>> > def __init__(self, shards=None, query_cls=CachingQuery, > >>> > engines_factory=None, **kwargs): > >>> > super(RoutingSession, > self).__init__(query_cls=query_cls, > >>> > **kwargs) > >>> > self.__binds = {} > >>
Re: [sqlalchemy] mapper existance checks possibly wrong
What does self._mapper_zero() return in your RoutingQuery._execute_and_instances method? Simon On Wed, Oct 25, 2017 at 11:52 AM, Антонио Антуан wrote: > Any news here? > > суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан > написал: >> >> I see that it is not happened when "bind" passed directly to >> "sessionmaker" >> >> сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : >>> >>> >>> >>> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >>> написал: On Fri, Oct 20, 2017 at 11:05 AM, Simon King wrote: > The "is not None" is important when checking a variable that may > contain a ClauseElement, precisely because ClauseElement defines that > __bool__ method. > > However, in Session.get_bind(), "mapper" is not supposed to contain a > ClauseElement. It should either be an instance of > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" is a > valid and concise way to write it. Comparing to None might be strictly > more accurate, but it shouldn't be necessary. agree, "mapper" means "mapper" and it is not supposed to be a ClauseElement. This sounds like arguments are not being passed correctly somewhere. Would need a demonstration script if something in SQLAlchemy itself is claimed to be making the mistake. >>> >>> >>> Ok, that code produces mentioned error: >>> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >>> Traceback (most recent call last): >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >>> >>> Session.query(with_recursive).set_shard('default').all() >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2654, >>> in all >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2802, >>> in __iter__ >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >>> _execute_and_instances >>> shard_id=self._shard_id).execute( >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2806, >>> in _connection_from_session >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> 984, in connection >>> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in >>> get_bind >>> original_bind = super(RoutingSession, self).get_bind(mapper, clause) >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >>> 1336, in get_bind >>> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line >>> 539, in __bool__ >>> TypeError: Boolean value of this clause is not defined >>> > > Simon > > > > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан > wrote: >> Really, `mapper` contains this: >>> >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >> already >> encountered this problem in my code, when I checked sqla-objects >> existance >> without "is not None", project was broken. Is it normal to omit that >> condition in sqlalchemy code? >> >> I use: > sqlalchemy.__version__ >> '1.0.19' >> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon King >> написал: >>> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан >>> wrote: >>> > Hi. >>> > I use my own `RoutingSession` and `RoutingQuery` implementation, >>> > most of >>> > it >>> > inspired by `sqlalchemy.ext.horizontal_shard`: >>> > >>> > class RoutingSession(Session): >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, >>> > **kwargs): >>> > original_bind = None >>> > try: >>> > original_bind = super(RoutingSession, >>> > self).get_bind(mapper, >>> > clause) >>> > except UnboundExecutionError: >>> > # may not be bound >>> > pass >>> > if shard_id is None: >>> > shard_id = TenantIDStorage.get_shard_id() # just >>> > global >>> > storage >>> > bind_for_shard = self.__binds[shard_id] >>> > if original_bind is not None and original_bind.url == >>> > bind_for_shard.url: >>> > return original_bind >>> > else: >>> > return bind_for_shard >>> > >>> > def __init__(self, shards=None, query_cls=CachingQuery, >>> > engines_factory=None, **kwargs): >>> > super(RoutingSession, self).__init__(query_cls=query_cls, >>> > **kwargs) >>> > self.__binds = {} >>> > self.engines_factory = engines_factory >>> > if shards is not None: >>> > self.update_shards(**shards) >>> > >>> > def _add_bind(self, key, bind): >>> > self.__binds[key] = bind >>> > >>> > >>> > >>> > >>> > class RoutingQuery(Query): >>> > def __init__(s
Re: [sqlalchemy] mapper existance checks possibly wrong
Any news here? суббота, 21 октября 2017 г., 18:42:47 UTC+3 пользователь Антонио Антуан написал: > > I see that it is not happened when "bind" passed directly to > "sessionmaker" > > сб, 21 окт. 2017 г. в 18:33, Антонио Антуан : > >> >> >> пятница, 20 октября 2017 г., 20:50:52 UTC+3 пользователь Mike Bayer >> написал: >> >>> On Fri, Oct 20, 2017 at 11:05 AM, Simon King >>> wrote: >>> > The "is not None" is important when checking a variable that may >>> > contain a ClauseElement, precisely because ClauseElement defines that >>> > __bool__ method. >>> > >>> > However, in Session.get_bind(), "mapper" is not supposed to contain a >>> > ClauseElement. It should either be an instance of >>> > sqlalchemy.orm.mapper.Mapper, or None, in which case "if mapper:" is a >>> > valid and concise way to write it. Comparing to None might be strictly >>> > more accurate, but it shouldn't be necessary. >>> >>> agree, "mapper" means "mapper" and it is not supposed to be a >>> ClauseElement. This sounds like arguments are not being passed >>> correctly somewhere. Would need a demonstration script if something >>> in SQLAlchemy itself is claimed to be making the mistake. >>> >> >> Ok, that code produces mentioned error: >> https://gist.github.com/aCLr/113ac292c05bdb01e964d8e9884d6e5f >> Traceback (most recent call last): >> File "/home/anton/Projects/proj/core/run/stuff.py", line 100, in >> >> Session.query(with_recursive).set_shard('default').all() >> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2654, >> in all >> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2802, >> in __iter__ >> File "/home/anton/Projects/proj/core/run/stuff.py", line 28, in >> _execute_and_instances >> shard_id=self._shard_id).execute( >> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/query.py", line 2806, >> in _connection_from_session >> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> 984, in connection >> File "/home/anton/Projects/proj/core/run/stuff.py", line 40, in get_bind >> original_bind = super(RoutingSession, self).get_bind(mapper, clause) >> File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py", line >> 1336, in get_bind >> File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/elements.py", line >> 539, in __bool__ >> TypeError: Boolean value of this clause is not defined >> >> >>> > >>> > Simon >>> >> > >>> > >>> > >>> > On Fri, Oct 20, 2017 at 3:17 PM, Антонио Антуан >>> wrote: >>> >> Really, `mapper` contains this: >> >> 0x7fe673fb0a50; group_getter>. But does it changes something? I >>> already >>> >> encountered this problem in my code, when I checked sqla-objects >>> existance >>> >> without "is not None", project was broken. Is it normal to omit that >>> >> condition in sqlalchemy code? >>> >> >>> >> I use: >>> > sqlalchemy.__version__ >>> >> '1.0.19' >>> >> >>> >> пятница, 20 октября 2017 г., 16:42:23 UTC+3 пользователь Simon King >>> написал: >>> >>> >>> >>> On Fri, Oct 20, 2017 at 2:15 PM, Антонио Антуан >>> wrote: >>> >>> > Hi. >>> >>> > I use my own `RoutingSession` and `RoutingQuery` implementation, >>> most of >>> >>> > it >>> >>> > inspired by `sqlalchemy.ext.horizontal_shard`: >>> >>> > >>> >>> > class RoutingSession(Session): >>> >>> > def get_bind(self, mapper=None, clause=None, shard_id=None, >>> >>> > **kwargs): >>> >>> > original_bind = None >>> >>> > try: >>> >>> > original_bind = super(RoutingSession, >>> self).get_bind(mapper, >>> >>> > clause) >>> >>> > except UnboundExecutionError: >>> >>> > # may not be bound >>> >>> > pass >>> >>> > if shard_id is None: >>> >>> > shard_id = TenantIDStorage.get_shard_id() # just >>> global >>> >>> > storage >>> >>> > bind_for_shard = self.__binds[shard_id] >>> >>> > if original_bind is not None and original_bind.url == >>> >>> > bind_for_shard.url: >>> >>> > return original_bind >>> >>> > else: >>> >>> > return bind_for_shard >>> >>> > >>> >>> > def __init__(self, shards=None, query_cls=CachingQuery, >>> >>> > engines_factory=None, **kwargs): >>> >>> > super(RoutingSession, self).__init__(query_cls=query_cls, >>> >>> > **kwargs) >>> >>> > self.__binds = {} >>> >>> > self.engines_factory = engines_factory >>> >>> > if shards is not None: >>> >>> > self.update_shards(**shards) >>> >>> > >>> >>> > def _add_bind(self, key, bind): >>> >>> > self.__binds[key] = bind >>> >>> > >>> >>> > >>> >>> > >>> >>> > >>> >>> > class RoutingQuery(Query): >>> >>> > def __init__(self, *args, **kwargs): >>> >>> > super(RoutingQuery, self).__init__(*args, **kwargs) >>> >>> > self._shard_id = TenantIDStorage.get_shard_id() >>> >>> > >>> >>> > def get(self, ident): >>> >>> > s