Re: [sqlalchemy] Strange issue with unicode
I've posted the answer on StackOverflow. You have client_encoding = sql_ascii in your postgresql.conf, and you just need to change that to utf8 or override it in your create_engine call. Cheers, Gulli On Wed, Jul 23, 2014 at 6:59 PM, Michael Bayer mike...@zzzcomputing.com wrote: On Jul 23, 2014, at 1:20 PM, Andrew Pashkin andrew.pash...@gmx.co.uk wrote: Hi all! I have an issue with unicode and SQLAlchemy. I've created topic on SO: http://stackoverflow.com/questions/24795444/how-to-save-unicode-with-sqlalchemy And repository with Vagrant/Ansible setup, so you can easily reproduce this bug on your local machine: https://github.com/AndrewPashkin/sqlalchemy_pg_unicode_save_fail In short - I just get UnicodeEncodeError when trying to save one specific Unicode symbol ('\u2013') with SQLAlchemy. Does anybody know how this can be handled? ps The most weird thing here is that, author of this test case: http://stackoverflow.com/a/24798089/1818608 does not get such error, as I am. do you have a stack trace? C extensions are installed or not? What OS platform? What’s the client encoding of your PG database (it’s in postgresql.conf). Try creating a raw psycopg2 script? -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] dogpile with SA inheritance
Hi Pavel, You want: s.query(Person).with_polymorphic(Man).get(51) Cheers, Gulli On Tue, Apr 15, 2014 at 12:59 PM, Pavel Aborilov abori...@gmail.com wrote: Hello! How can I cache query like this: session.query(Person).get(51) where 51 is id of Man I can't access attribute age of Man without SELECT. Models: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Man(Person): __tablename__ = 'man' id = Column(Integer, ForeignKey('person.id'), primary_key=True) age = Column(String(100), nullable=False) __mapper_args__ = {'polymorphic_identity': 'man'} -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] dogpile with SA inheritance
On Tue, Apr 15, 2014 at 1:11 PM, Pavel Aborilov abori...@gmail.com wrote: but I dont know on the time of query what the type of object it will be. Then you can use session.query(Person).with_polymorphic('*') to mean joining to the tables of all mapped subclasses. (Be aware that this can become problematic if there is a lot of them.) Cheers, Gulli On Tuesday, April 15, 2014 5:06:48 PM UTC+4, Gunnlaugur Briem wrote: Hi Pavel, You want: s.query(Person).with_polymorphic(Man).get(51) Cheers, Gulli On Tue, Apr 15, 2014 at 12:59 PM, Pavel Aborilov abor...@gmail.comwrote: Hello! How can I cache query like this: session.query(Person).get(51) where 51 is id of Man I can't access attribute age of Man without SELECT. Models: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Man(Person): __tablename__ = 'man' id = Column(Integer, ForeignKey('person.id'), primary_key=True) age = Column(String(100), nullable=False) __mapper_args__ = {'polymorphic_identity': 'man'} -- 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+...@googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Is it possible to create a filter 'string' LIKE column + '%'?
Hi, See ColumnElement docs: http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.expression.ColumnElement ... for your specific example you can call .like(...) on column clauses: print Column('foo', Text).like('bar%baz') foo LIKE :foo_1 More generally, if you wanted some operator other than LIKE, existing in your DB dialect but not yet in SQLAlchemy, then you can use .op(...): print Column('foo', Text).op('FNORD')('foo%') foo FNORD :foo_1 Cheers, Gulli On Thu, Apr 10, 2014 at 12:19 PM, Mark Bird mark.a.b...@gmail.com wrote: I can't seem to find a way to do this without passing raw SQL to .filter() I could just do: .filter(column == func.substring('string', 1, func.char_length(column))) but is it possible to do it with LIKE? I.e. I need to return all rows that match the beginning of a string, so for 'string' I could match 's', 'st', 'str', etc. Thanks, Mark. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Could anyone please help explain expired object in plain language?
It means that an object in memory (or some of its attributes), representing an entity in the DB, is no longer considered to reflect the state of that entity accurately because the entity may have changed in the DB. So next time attributes are read from the object, fresh DB state is queried. See http://docs.sqlalchemy.org/en/latest/orm/session.html#refreshing-expiringTo clear out the currently loaded state on an instance, the instance or its individual attributes can be marked as expired, which results in a reload to occur upon next access of any of the instance's attrbutes. Gulli On Sun, Feb 23, 2014 at 10:00 AM, Bao Niu niuba...@gmail.com wrote: I read the documentation several times yet still didn't find an official definition for expired object, although it is used quite often. To my understanding, it means when you update some attributes on a persistent object, so those affected attributes that are still lying in database become expired. Is my understanding correct, please? Thanks very much. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Is there a way to examine how many objects a session currently contains?
Hi, read the section Session Attributeshttp://docs.sqlalchemy.org/en/latest/orm/session.html#sqlalchemy.orm.session.Session.identity_map. You might just need something like len(session.identity_map) + len(session.new) ... but there are some caveats, involving exactly what you mean by the session holding an object (i.e. what you need this set for): - That assumes you want to include objects that have been added to the session but haven't yet been flushed to the database, so they do not yet have an identity - It also assumes you don't want to *exclude* objects which have been deleted in the session but whose deletion has not yet been flushed to the database; the session is still holding those objects - The identity_map is a weak-referencing map, so it may or may not still reference objects to which your application no longer holds a reference, depending on garbage collection. So you may get a different set before and after calling gc.collect(). Cheers, Gulli -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] ProgrammingError: cannot determine type of empty array even with proper model declaration?
Hi, 1. the server_default=... argument just says what default to define for the column *on creation* --- it has no effect if the table already exists. To apply the default to an existing table, you need to execute something like: ALTER TABLE mytbl ALTER COLUMN mycol ADD DEFAULT ARRAY[]::integer[]; 2. the server-side default has no effect if you configure SQLAlchemy to always specify a value for the column (which is what the default=... argument does). So you could either (a) alter the table and skip the default=... argument, or (b) change the default=... argument to literal SQL with an explicit cast, e.g. default=literal_column('{}'::integer[]) (because array() doesn't get compiled with an explicit typecast, even if you give it an explicit type_ ... maybe it should, when the array is empty.) Cheers, Gulli On Tue, Dec 3, 2013 at 1:10 PM, Michael Nachtigal michael.nachti...@catalinamarketing.com wrote: Hello, all, I'm receiving this error: ProgrammingError: (ProgrammingError) cannot determine type of empty array It looks like this is happening because the default value for column in new instances of one of my models is being initialized to [] (an empty array, no inner type), despite my column being defined like this in the model class: mycol = Column('mycol', ARRAY(Integer), server_default=text('ARRAY[]::integer[]'), default=array([], type_=Integer), nullable=False) I've tried it with both the default and server_default kwargs, with only the default kwarg, and with only the server_default kwargs, and it seems not to make a difference (and I'm not sure really which combination of arguments would be most appropriate here, anyway, but that's not the primary reason for my question). The column in the database is defined like this: mycol integer array NOT NULL I would appreciate any kind of help or information anyone could provide to help me solve this. Thanks, Mike -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] PG's JSON Data Type
There is something for this in SQLAlchemy-Utils, I noticed the other day: http://sqlalchemy-utils.readthedocs.org/en/latest/data_types.html#module-sqlalchemy_utils.types.json ... but I have no idea how useful or mature that is. Gulli On Mon, Nov 18, 2013 at 3:24 PM, Michael Bayer mike...@zzzcomputing.comwrote: as you’ve found on the issue tracker it’s a TODO, I’m surprised nobody’s contributed this yet as it follows the same patterns as HSTORE and RANGE, both of which were contributed by users. On Nov 17, 2013, at 6:46 PM, Adam Tauno Williams awill...@whitemice.org wrote: Has anyone implemented a wrapper for PostgreSQL's JSON data type? Particularly one that uses the built-in operations in 9.3. I have been pointed to https://github.com/inklesspen/frameline/blob/master/frameline/models.py but any use of operator's would have to be done as a literal. -- Adam Tauno Williams mailto:awill...@whitemice.org GPG D95ED383 Systems Administrator, Python Developer, LPI / NCLA -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] many queries select if in cycle has insert into table
The reason for the extra selects is that calling commit() marks objects in the session as expired, so they need to be refreshed. From the ORM tutorialhttp://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html : SQLAlchemy by default refreshes data from a previous transaction the first time it’s accessed within a new transaction, so that the most recent state is available. The level of reloading is configurable as is described in *Using the Session* http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html. From the section on committinghttp://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#committing : Another behavior of commit() is that by default it expires the state of all instances present after the commit is complete. This is so that when the instances are next accessed, either through attribute access or by them being present in a Query result set, they receive the most recent state. To disable this behavior, configure sessionmaker with expire_on_commit=False. The other option, is to just commit after the loop, not inside it. That is OK if the loop is not too long-running and there aren't tricky locking ramifications — and indeed it may be preferable if you want to make sure the ManagerConfigs you create are transactionally consistent with the BarLogs they are based on; for that you might also consider loading the BarLogs .with_lockmode('read') Gulli On Fri, Aug 30, 2013 at 8:10 AM, Дмитрий Косолапов kosolapo...@gmail.comwrote: my program code: engine = create_engine(connect_str, echo=True)Session = sessionmaker(bind=engine)for bar in default_session.query(BarLog)[:3]: conf = ManagerConfig(indicator_config='', timeframe=bar.timeframe, paper_no=1) default_session.add(conf) default_session.commit() log: 2013-08-29 22:52:58,640 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-08-29 22:52:58,642 INFO sqlalchemy.engine.base.Engine SELECT bar_log.id AS bar_log_id, bar_log.max AS bar_log_max, bar_log.min AS bar_log_min, bar_log.open AS bar_log_open, bar_log.close AS bar_log_close, bar_log.volume AS bar_log_volume, bar_log.time_start AS bar_log_time_start, bar_log.date AS bar_log_date, bar_log.timeframe AS bar_log_timeframe, bar_log.paper_no AS bar_log_paper_no FROM bar_log LIMIT %(param_1)s 2013-08-29 22:52:58,642 INFO sqlalchemy.engine.base.Engine {'param_1': 3} 2013-08-29 22:52:58,646 INFO sqlalchemy.engine.base.Engine INSERT INTO manager_config (paper_no, timeframe, indicator_config) VALUES (%(paper_no)s, %(timeframe)s, %(indicator_config)s) RETURNING manager_config.id 2013-08-29 22:52:58,646 INFO sqlalchemy.engine.base.Engine {'indicator_config': ‘', ’paper_no': 1, ‘timeframe’: 1} 2013-08-29 22:52:58,647 INFO sqlalchemy.engine.base.Engine COMMIT 2013-08-29 22:52:58,662 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-08-29 22:52:58,664 INFO sqlalchemy.engine.base.Engine SELECT bar_log.id AS bar_log_id, bar_log.max AS bar_log_max, bar_log.min AS bar_log_min, bar_log.open AS bar_log_open, bar_log.close AS bar_log_close, bar_log.volume AS bar_log_volume, bar_log.time_start AS bar_log_time_start, bar_log.date AS bar_log_date, bar_log.timeframe AS bar_log_timeframe, bar_log.paper_no AS bar_log_paper_no FROM bar_log WHERE bar_log.id = %(param_1)s 2013-08-29 22:52:58,664 INFO sqlalchemy.engine.base.Engine {'param_1': 2} 2013-08-29 22:52:58,667 INFO sqlalchemy.engine.base.Engine INSERT INTO manager_config (paper_no, timeframe, indicator_config) VALUES (%(paper_no)s, %(timeframe)s, %(indicator_config)s) RETURNING manager_config.id 2013-08-29 22:52:58,668 INFO sqlalchemy.engine.base.Engine {'indicator_config': ‘', ’paper_no': 1, ‘timeframe’: 1} 2013-08-29 22:52:58,670 INFO sqlalchemy.engine.base.Engine COMMIT 2013-08-29 22:52:58,679 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-08-29 22:52:58,681 INFO sqlalchemy.engine.base.Engine SELECT bar_log.id AS bar_log_id, bar_log.max AS bar_log_max, bar_log.min AS bar_log_min, bar_log.open AS bar_log_open, bar_log.close AS bar_log_close, bar_log.volume AS bar_log_volume, bar_log.time_start AS bar_log_time_start, bar_log.date AS bar_log_date, bar_log.timeframe AS bar_log_timeframe, bar_log.paper_no AS bar_log_paper_no FROM bar_log WHERE bar_log.id = %(param_1)s 2013-08-29 22:52:58,681 INFO sqlalchemy.engine.base.Engine {'param_1': 3} 2013-08-29 22:52:58,685 INFO sqlalchemy.engine.base.Engine INSERT INTO manager_config (paper_no, timeframe, indicator_config) VALUES (%(paper_no)s, %(timeframe)s, %(indicator_config)s) RETURNING manager_config.id 2013-08-29 22:52:58,685 INFO sqlalchemy.engine.base.Engine {'indicator_config': ‘', ’paper_no': 1, ‘timeframe’: 1} 2013-08-29 22:52:58,688 INFO sqlalchemy.engine.base.Engine COMMIT how to use one select query and many insert queries? -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails
Re: [sqlalchemy] sqlite string concats and datetime arithmetics
Oh, that's what it is. The override of the addition + operator by the concatenation operator || only happens if the right-hand side type is also a “concatenable” or NULL, not just the left side: https://github.com/zzzeek/sqlalchemy/blob/rel_0_8_2/lib/sqlalchemy/types.py#L1017-L1023 and integer types are not marked as concatenable. Minimal test of this: from sqlalchemy.types import Text, Integer from sqlalchemy.sql import literal_column print literal_column('foo', type_=Text) + literal_column('bar', type_=Text) print literal_column('foo', type_=Text) + literal_column(3, type_=Integer) This prints: 'foo' || 'bar' 'foo' + 3 Not sure if it would be unambiguously correct to apply the concatenation override whenever the left side is a concatenable; seems superficially like it might be so, but there may well be cases where that's a problem. If so, this is probably not a bug. In any case, you can sidestep the whole issue of how + is interpreted, by being explicit about what you meant, using .concat(): print literal_column('foo', type_=Text).concat(literal_column(3, type_=Integer)) which prints what you wanted: 'foo' || 3 Gulli On Fri, Aug 30, 2013 at 3:34 PM, Florian Rüchel florian.ruec...@gmail.comwrote: I recently had exactly the same problem on SQLAlchemy 0.8 where doing something like somestring + Item.some_column would not result in a || but in a + operator which sqlite could not handle. To note here in my case: The some_column was the id, thus an integer. I'm not sure how SQLAlchemy handles the operators exactly, but could this be a bug? Or is it intended behaviour? On Thursday, August 29, 2013 8:58:55 PM UTC+2, Gunnlaugur Briem wrote: I would have expected the SQLite dialect to know how to compile concatto || if that's the operator. But failing that, something more explicit like this ought to do the trick: from sqlalchemy.sql import literal_column literal_column('+ ').op('||')(seconds.c.n).op('**||')(literal_column(' seconds')) Gulli On Thu, Aug 29, 2013 at 12:33 PM, Greg Yang sorcer...@gmail.com wrote: I'm trying to get a series of datetimes using func.datetime. The format of input is func.datetime(basetime, '+ NNN seconds'), which works nicely if the shift applied is constant. However I need to add 10, 20, 30 seconds, etc to this base time. So I want something like func.datetime(basetime, concat('+', seconds.c.n, ' seconds')), but concat doesn't work for sqlite, which concatenates using the '||' operator. Is there working method to concat in sqlite? Failing that, is there another way to get at what I want with datetime arithmetics? -- 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+...@**googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at http://groups.google.com/**group/sqlalchemyhttp://groups.google.com/group/sqlalchemy . For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out . -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] sqlite string concats and datetime arithmetics
I would have expected the SQLite dialect to know how to compile concat to ||if that's the operator. But failing that, something more explicit like this ought to do the trick: from sqlalchemy.sql import literal_column literal_column('+ ').op('||')(seconds.c.n).op('||')(literal_column(' seconds')) Gulli On Thu, Aug 29, 2013 at 12:33 PM, Greg Yang sorcerero...@gmail.com wrote: I'm trying to get a series of datetimes using func.datetime. The format of input is func.datetime(basetime, '+ NNN seconds'), which works nicely if the shift applied is constant. However I need to add 10, 20, 30 seconds, etc to this base time. So I want something like func.datetime(basetime, concat('+', seconds.c.n, ' seconds')), but concat doesn't work for sqlite, which concatenates using the '||' operator. Is there working method to concat in sqlite? Failing that, is there another way to get at what I want with datetime arithmetics? -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Calculate birthdays
To clarify: - The class-level attribute Member.dateofbirth is not a date/datetime object. It is a instrumented attribute representing a column in the database table behind this model. So it does not have any method called replace. - Once you get an *instance* of Member, the instance-level attribute dateofbirth will be of python type date/datetime and will have a replace method. - Because this was not clear to you, you should really really go through the introductory tutorial (it's quite good and will bring you up to speed): http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html Gulli On Wed, Aug 28, 2013 at 5:54 PM, Jonathan Vanasco jonat...@findmeon.comwrote: On Wednesday, August 28, 2013 12:52:03 PM UTC-4, herzaso wrote: What's wrong with Member.dateofbirth==datetime.**today() ? datetime.today() is now -- or August 28, 2013 12:52:03 PM UTC-4 The OP wants a sql operation that matches the Month+Day of Member.dateofbirth to the Month+day of today. See this thread for how to use `datepart` -- https://groups.google.com/forum/#!msg/sqlalchemy/PaDkSHA4zcw/xTp1DjVQZOAJ -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Calculate birthdays
sorry, it looks like the OP did want people born on the current month/day/year combo. No, you were right the first time : ) ... he wanted members whose dateofbirth, *after changing the year to the current year*, would be today. That amounts to equating the month and day only. Something like this would do that: from sqlalchemy.sql.expression import extract members_with_birthday_today = Member.query.filter( (extract(MONTH, Member.dateofbirth) == today.month) (extract(DAY, Member.dateofbirth) == today.day)) ).all() Gulli On Wed, Aug 28, 2013 at 6:01 PM, Jonathan Vanasco jonat...@findmeon.comwrote: sorry, it looks like the OP did want people born on the current month/day/year combo. you should be able to wrap all the comparisons in a date like this : Member.query.filter( sqlalchemy.func.date(Member.**dateofbirth) == '2013-08-27' ).all() Member.query.filter( sqlalchemy.func.date(Member.**dateofbirth) == sqlalchemy.func.date(datetime.today()) ).all() -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?
Yep: object_session(obj) http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#sqlalchemy.orm.session.Session.object_session Regards, Gulli On Thu, May 2, 2013 at 4:47 PM, Jonathan Vanasco jonat...@findmeon.comwrote: i'm trying to deal with some old code , and need to 'log' a change. in the current code block, I do not have a SqlAlchemy session object - i merely have an ORM object that exists in a given session. is it possible to extract the session object from that ORM object, so I can just add a new ORM object to it? doing the correct thing, and getting the session object into the code block properly will take more time than I have right now. -- 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 http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?
On Thu, May 2, 2013 at 5:08 PM, Michael Bayer mike...@zzzcomputing.comwrote: and in 0.8 its this: from sqlalchemy import inspect session = inspect(obj).session Is this preferred over object_session in 0.8? object_session is still there, and docs don't say it's deprecated or that inspect(obj).session is preferred. Gulli -- 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 http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] [Q] WindowedRangeQuery recipe
No, the comma is supposed to be there; it's for tuple unpacking. The iterable q yields tuples (which in this case are of length one, because the resultset has only one column). The column should be whatever attribute of the ORM instances you want to sort by, not necessarily the primary key. The ORM instances are referenced by the session, so they will not be reclaimed until the session is closed (or they are expunged from it). Regards, Gulli On Fri, Apr 26, 2013 at 10:06 AM, Ladislav Lenart lenart...@volny.czwrote: Hello. I have found this recipe: http://www.sqlalchemy.org/trac/wiki/UsageRecipes/WindowedRangeQuery I think it fits perfectly for my usecase: process potentially large result set of ORM instances in chunks of predefined size to limit memory consumption. I have few questions / remarks: * I think there is a typo in the line (the comma should not be there, right?): intervals = [id for id, in q] * The column supplied to windowed_query() function should be the primary key of the table that represents the ORM instances, right? * When are the ORM instances from the previous chunk reclaimed? I know the session keeps them in an identity map. Thank you, Ladislav Lenart -- 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 http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] Re: Late and ugly error when mixing timezone-savvy and timezone-naive datetimes
OK, so it's the database driver that should make sure values suit the columns they are bound to, but @validates allows me to do it too. Makes perfect sense — thanks! Regards, - Gulli On Thu, Mar 12, 2009 at 2:28 PM, Michael Bayer mike...@zzzcomputing.comwrote: Gunnlaugur Briem wrote: Hi, I get away with stuffing datetime.datetime.now() into a DateTime (timezone=True) column, despite the former being timezone-naive (.utcoffset() is None, .tzinfo is None, etc.). It is stored in the table with UTC offset +00, which is arguably incorrect (states information that was not present in the input). But even if you call it correct, you get in trouble when you read the value back as an attribute of a mapped class in a session, set the attribute again to datetime.datetime.now() (again timezone-naive), and then try to query the session for the same object again. This retches up a TypeError: “can't compare offset-naive and offset-aware datetimes”. SQLA doesn't process datetime objects at all when using the postgres dialect - psycopg2 supports datetime objects directly and sqlalchemy passes them straight through. you should use only timezone-aware datetime objects if you are dealing with a column of that type. If you'd like to add validation or processing specific to your use case, you can use the @validates decorator at the ORM level or the TypeDecorator at the table metadata level to provide whatever rulesets you'd like. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Why do I have to begin a transaction?
See: http://www.sqlalchemy.org/docs/05/reference/orm/sessions.html#sqlalchemy.orm.create_session “The defaults of create_session() are the opposite of that of sessionmaker(); autoflush and expire_on_commit are False, autocommit is True. [...] It is recommended to use sessionmaker() instead of create_session().” Isn't that it? - G. On Wed, Mar 4, 2009 at 7:21 PM, Mike Orr sluggos...@gmail.com wrote: I have a standalone utility using a model based on Declarative on MySQL using SQLAlchemy 0.5.2. Most of the code is at the SQL level but at one point I use the ORM to update or insert a summary record. So I figured I'd use create_session because it's a single-threaded utility. === sess = orm.create_session(bind=conn) q = sess.query(model.Monthly).filter_by(...) monthly = q.first() if not monthly: monthly = model.Monthly() ... sess.add(monthly) ... sess.commit() === That raises sqlalchemy.exc.InvalidRequestError: No transaction is begun. To work around that I have to put sess.begin() after creating the session. But why? I don't have to do this when using scoped_session in Pylons. The SQLAlchemy docs seem to say that it automatically manages transactions if you don't change the default session arguments, and that this works identically with Session, create_session, and sessionmaker. So why is it behaving differently here? -- Mike Orr sluggos...@gmail.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Infinite recursion in sqlalchemy/orm/attributes.py when running under debugger
The context of the infinite recursion is this: def __getattr__(self, attribute): Delegate __getattr__ to the original descriptor and/or comparator. try: return getattr(descriptor, attribute) except AttributeError: try: return getattr(self._comparator, attribute) --- recursion here except AttributeError: raise AttributeError('Neither %r object nor %r object has an attribute %r' % ( type(descriptor).__name__, type(self._comparator).__name__, attribute) ) I added a line to print the AttributeError that comes up. The first time, it is 'property' object has no attribute '_update_table_name'. So the method _update_table_name is being looked up on the property object (or proxy, here), instead of the instance of class Foo. This may be because of not-so-minimally invasive surgery on the part of the debugger. The rest of the time (in infinite recursion), the AttributeError is: 'property' object has no attribute '_comparator'. So evaluating self._comparator involves calling self.__getattr__('_comparator'), presumably because the self_comparator member is missing for some reason (also quite possibly something involving debugger hooks). I get around this by changing self._comparator to self.__dict__('_comparator') in both places, above. That way, the error: AttributeError: Neither 'property' object nor 'NoneType' object has an attribute '_update_table_name' is raised, which still leaves something awry under debugging (looking up the attribute in the wrong object), but at least there is no infinite recursion. A patch against trunk is attached, if you want to apply this change. Possibly it is not really needed when the other problem is fixed (attribute looked up in the wrong object when debugging), but I believe it is more robust, and all SQLAlchemy tests pass. Regards, - Gulli On Tue, Mar 3, 2009 at 4:10 AM, Gunnlaugur Briem gunnlau...@gmail.comwrote: Hi, I have a table-mapped attribute that is dependent on two other attributes: from sqlalchemy import Table, MetaData, Column, Text, create_engine, Integer from sqlalchemy.orm import mapper, synonym class Foo(object): def _get_name(self): return self._name def _set_name(self, name): self._name = name self._update_table_name() name = property(_get_name, _set_name) def _get_provider(self): return self._provider def _set_provider(self, provider): self._provider = provider self._update_table_name() provider = property(_get_provider, _set_provider) def _update_table_name(self): table_name = %s_%s % (self.provider, self.name) if len(table_name) 50: table_name = table_name[0:50] self.table_name = table_name foo_table = Table('foo', MetaData(), Column('id', Integer, primary_key=True), Column('name', Text), Column('provider', Text), Column('table_name', Text) ) mapper(Foo, foo_table, properties={ 'name' : synonym('_name', map_column=True), 'provider': synonym('_provider', map_column=True), }) e = create_engine('sqlite:///:memory:') foo_table.metadata.create_all(bind=e) When I run this normally, nothing happens. When I run it under the debugger (in PyDev), I get infinite recursion, looking like this: Traceback (most recent call last): File /Applications/eclipse/plugins/ org.python.pydev.debug_1.4.4.2636/pysrc/pydevd.py, line 883, in module debugger.run(setup['file'], None, None) File /Applications/eclipse/plugins/ org.python.pydev.debug_1.4.4.2636/pysrc/pydevd.py, line 712, in run execfile(file, globals, locals) #execute the script File /Users/gthb/Documents/workspace/test/src/sqlalchemytest7.py, line 33, in module 'provider': synonym('_provider', map_column=True), File /path/to/SQLAlchemy/sqlalchemy/orm/__init__.py, line 752, in mapper return Mapper(class_, local_table, *args, **params) File /path/to/SQLAlchemy/sqlalchemy/orm/mapper.py, line 198, in __init__ self._configure_properties() File /path/to/SQLAlchemy/sqlalchemy/orm/mapper.py, line 481, in _configure_properties self._configure_property(key, prop, False) File /path/to/SQLAlchemy/sqlalchemy/orm/mapper.py, line 616, in _configure_property prop.instrument_class(self) File /path/to/SQLAlchemy/sqlalchemy/orm/properties.py, line 302, in instrument_class proxy_property=self.descriptor File /path/to/SQLAlchemy/sqlalchemy/orm/attributes.py, line 1590, in register_descriptor descriptor = proxy_type(key, proxy_property, comparator, parententity) File /path/to/SQLAlchemy/sqlalchemy/orm/attributes.py, line 181, in __init__
[sqlalchemy] Re: Creating SQL Expression
You can get the column object from the string, using: xyz.c[column_name] Do you mean that this: columns = [a,b,c] operators = ['+','-'] should result in xyz.c.a + xyz.c.b - xyz.c.c ? To do that, something like this works: columns = [a,b,c] operators = ['+','-'] colnames_and_ops = zip(columns[1:], operators) import operator opdict = { '+' : operator.add, '-' : operator.sub } columns = [xyz.c[name] for name in columns] operators = [opdict[name] for name in operators] ops_cols = zip(operators, columns[1:]) expr = reduce(lambda expr, op_col: op_col[0](expr, op_col[1]), ops_cols, columns[0]) Hope that helps, - Gulli On Thu, Feb 26, 2009 at 5:47 AM, Ashish Bhatia ashishsinghbha...@gmail.comwrote: This works fine But in the mine case columns = [a,b,c] operator = ['+','-'] comes in the list And it can go to n number. So while adding it creates a problem My approach looping on columns i append it in to the table and hence making the object i can join them with operator to form the a+b-c but in this a b c becomes string which is not desirable i want object here i hope this will clear the picture On Feb 25, 6:40 pm, Gunnlaugur Thor Briem gunnlau...@gmail.com wrote: You can sum the column objects directly, a+b, producing a sqlalchemy.sql.expression._BinaryExpression object. t = Table('bobloblaw', MetaData(), Column('a', Integer), Column('b', Integer), Column('c', Integer)) t.c.a + t.c.b # evaluates to sqlalchemy.sql.expression._BinaryExpression object at 0x1ec9ff0 print t.c.a + t.c.b # bobloblaw.a + bobloblaw.b On Wed, Feb 25, 2009 at 1:25 PM, Ashish Bhatia ashishsinghbha...@gmail.comwrote: The problem is still their. The two seprate list of columns = List of sqlalchem object operator = ['+'','-'] using join to join them will convert the columns object to string which is not desirable. Any way to fix this. On Feb 25, 3:54 pm, Ashish Bhatia ashishsinghbha...@gmail.com wrote: sorry its resolved and working On Feb 25, 12:20 pm, Ash ashishsinghbha...@gmail.com wrote: Hello , I am trying to make query like select (a+b) from xyz; to do this xyz = sqlalchemy.Table('xyz',metadata) a = sqlalchemy.Column('a', sqlalchemy.Integer) xyz.append_column(a) b = sqlalchemy.Column('b', sqlalchemy.Integer) xyz.append_column(b) column = [(a + b)] select = sqlalchemy.select(from_obj=xyz, columns=column,distinct=True) This works fine for me. Now when the columns a and b are dynamic (Enter by the user in form of string) and the operator too comes from user columns_list = ['a','b'] operator = ['+'] like this i get the input so i make the loop and make for both the columns something like this columns = [] for x in column_list : t = sqlalchemy.Column(x, sqlalchemy.Integer) xyz.append_column(a) columns.append(t) so now how to add + to make the quer run Thanks in the advance. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Ordering results of a WHERE x in y query by y
Thanks. But using a CASE clause becomes objectionable in exactly those cases where I would want to have the DB do the sorting — i.e. where the table is big enough that just sorting the result set in python code using array index (rows.sort(key=lambda row: values.index(row[0]))) would be a Bad Thing (since the key function is O(n)). But then, sorting on a reversed enumeration dict in python is algorithmically the same as the temp table approach. Something like: rows = session.query(...).all() value_to_index = dict((v,k) for (k,v) in enumerate(values)) rows.sort(key=lambda value: value_to_index[value]) so I suppose that's the cleanest solution here, unless one really prefers to make the DB do the sorting. I believe all of these approaches will gracefully handle the case where values are not unique (the order will just be arbitrary within each group with the same value). Regards, - Gulli On Thu, Feb 26, 2009 at 9:40 AM, Ants Aasma ants.aa...@gmail.com wrote: import sqlalchemy def index_in(col, valuelist): return sqlalchemy.case([(value,idx) for idx,value in enumerate (valuelist)], value=col) session.query(C).filter(C.someattr.in_(valuelist)).order_by(index_in (C.someattr, valuelist)) Don't try to do this with huge lists of items. On Feb 25, 5:53 pm, Gunnlaugur Briem gunnlau...@gmail.com wrote: Hi all, having a x IN y query, with y supplied as input to the query: session.query(C).filter(C.someattr.in_(valuelist)) is there a way to tell SQLAlchemy to order the results according to valuelist? I.e. not by the natural order of someattr, but by the arbitrary order seen in valuelist? E.g.: session.add(C(someattr='Abigail')) session.add(C(someattr='Benjamin')) session.add(C(someattr='Carl')) valuelist = ['Benjamin', 'Abigail'] q = session.query(C).filter(C.someattr.in_(valuelist)).order_by(clever (valuelist)) q.all() # returns [C('Benjamin'), C('Abigail')] The solution I can think of is to create a temporary table with sess.execute('create temp table ...'), insert the valuelist into that temp table along with a sequence index, join to that temporary table and order by its index. Is there a less kludgy way? Regards, - Gulli --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Creating SQL Expression
Whoops, premature send, sorry. For an arbitrary list of columns, such as t.c (the column collection) or other SQL selectables, such as the above binary expressions, you can use sum(columns). E.g.: t = Table('bobloblaw', MetaData(), Column('a', Integer), Column('b', Integer), Column('c', Integer)) e = create_engine('sqlite:///:memory:') e.execute(t.insert(), [{'a':1, 'b':2, 'c':4}]) e.execute(select([sum(t.c)])).fetchall() # equiv. to SELECT a+b+c FROM t # [(7,)] Regards, - Gulli On Wed, Feb 25, 2009 at 1:40 PM, Gunnlaugur Thor Briem gunnlau...@gmail.com wrote: You can sum the column objects directly, a+b, producing a sqlalchemy.sql.expression._BinaryExpression object. t = Table('bobloblaw', MetaData(), Column('a', Integer), Column('b', Integer), Column('c', Integer)) t.c.a + t.c.b # evaluates to sqlalchemy.sql.expression._BinaryExpression object at 0x1ec9ff0 print t.c.a + t.c.b # bobloblaw.a + bobloblaw.b On Wed, Feb 25, 2009 at 1:25 PM, Ashish Bhatia ashishsinghbha...@gmail.com wrote: The problem is still their. The two seprate list of columns = List of sqlalchem object operator = ['+'','-'] using join to join them will convert the columns object to string which is not desirable. Any way to fix this. On Feb 25, 3:54 pm, Ashish Bhatia ashishsinghbha...@gmail.com wrote: sorry its resolved and working On Feb 25, 12:20 pm, Ash ashishsinghbha...@gmail.com wrote: Hello , I am trying to make query like select (a+b) from xyz; to do this xyz = sqlalchemy.Table('xyz',metadata) a = sqlalchemy.Column('a', sqlalchemy.Integer) xyz.append_column(a) b = sqlalchemy.Column('b', sqlalchemy.Integer) xyz.append_column(b) column = [(a + b)] select = sqlalchemy.select(from_obj=xyz, columns=column,distinct=True) This works fine for me. Now when the columns a and b are dynamic (Enter by the user in form of string) and the operator too comes from user columns_list = ['a','b'] operator = ['+'] like this i get the input so i make the loop and make for both the columns something like this columns = [] for x in column_list : t = sqlalchemy.Column(x, sqlalchemy.Integer) xyz.append_column(a) columns.append(t) so now how to add + to make the quer run Thanks in the advance. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: newbie problem
Works for me (once I add metadata.create_all(bind=engine) ) ... possibly you have an old SQLite that doesn't do the auto-incrementing primary key thing? - G. On Wed, Feb 18, 2009 at 2:26 PM, Marcin Krol mrk...@gmail.com wrote: Hello, I just started learning sqlalchemy, my version is 0.5.2, I'm reading the tutorial (Object Relational Tutorial) and produced this code: from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import mapper, sessionmaker engine = create_engine('sqlite:test.sqlite',echo=True) metadata = MetaData() users_table = Table('users', metadata, Column('id',Integer,primary_key=True), Column('name',String), Column('fullname',String), Column('password',String)) class User(object): def __init__(self,name,fullname,password): self.name = name self.fullname = fullname self.password = password mapper(User, users_table) ed_user = User('ed','Ed Jones','sdffa') Session=sessionmaker(bind=engine) session=Session() session.add(ed_user) session.commit() our_user=session.query(User).filter_by(name='ed').first() res = ed_user is our_user print res However, it fails due to exception: Traceback (most recent call last): File C:/Python26/userssqa.py, line 31, in module session.commit() File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py, line 673, in commit self.transaction.commit() File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py, line 378, in commit self._prepare_impl() File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py, line 362, in _prepare_impl self.session.flush() File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py, line 1347, in flush self._flush(objects) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py, line 1417, in _flush flush_context.execute() File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py, line 244, in execute UOWExecutor().execute(self, tasks) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py, line 707, in execute self.execute_save_steps(trans, task) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py, line 722, in execute_save_steps self.save_objects(trans, task) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py, line 713, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\mapper.py, line 1352, in _save_obj c = connection.execute(statement.values(value_params), params) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py, line 824, in execute return Connection.executors[c](self, object, multiparams, params) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py, line 874, in _execute_clauseelement return self.__execute_context(context) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py, line 896, in __execute_context self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py, line 950, in _cursor_execute self._handle_dbapi_exception(e, statement, parameters, cursor, context) File c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py, line 931, in _handle_dbapi_exception raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) IntegrityError: (IntegrityError) users.id may not be NULL u'INSERT INTO users (name, fullname, password) VALUES (?, ?, ?)' ['ed', 'Ed Jones', 'sdffa'] This is pretty obvious, 'id' integer column has not been filled. But I have no idea how to remedy this, since this seems to be dependent on smth in the guts of sqlalchemy. Help! Regards, mk --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] polymorphic_identity not allowed to be zero for base class?
Hi, [trying to send this again, seems like previous copy got lost in some moderation queue] Messing with single-table inheritance in a declarative model, with a non-abstract base class, I find that querying fails if polymorphic_identity is 0 (zero). Example: code begins from sqlalchemy import Column, Integer, create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() class Course(Base): __tablename__ = 'course' id = Column(Integer, primary_key=True) course_type = Column(Integer) __mapper_args__ = {'polymorphic_on':course_type, 'polymorphic_identity':0} class MultiYearCourse(Course): __mapper_args__ = {'polymorphic_identity':1} engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(bind=engine) session = sessionmaker(bind=engine)() myc = MultiYearCourse() myc.name = uComputer Graphics c = Course() c.name = uSociology session.add(c) session.add(myc) session.commit() print MYC: %s % myc print C: %s % c query = session.query(Course) print Query: %s % query print Results: %s % query.all() code ends That last line fails with an AssertionError: output begins MYC: __main__.MultiYearCourse object at 0xcf7d30 C: __main__.Course object at 0xcf7d70 Query: SELECT course.id AS course_id, course.course_type AS course_course_type FROM course Traceback (most recent call last): File /Users/gthb/Documents/workspace/test/src/sqlalchemytest.py, line 31, in module print Results: %s % query.all() File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1186, in all return list(self) File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1341, in instances rows = [process[0](context, row) for row in fetch] File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1942, in main return _instance(row, None) File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py, line 1557, in _instance _instance = polymorphic_instances[discriminator] File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/util.py, line 71, in __missing__ self[key] = val = self.creator(key) File /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py, line 1695, in configure_subclass_mapper raise AssertionError(No such polymorphic_identity %r is defined % discriminator) AssertionError: No such polymorphic_identity 0 is defined output ends But if I exchange the polymorphic identities, so the base class gets the 1 and the subclass gets the 0, then it runs just fine! It seems to me that this can't be intentional — don't see a reason for it, and the docs do not mention any particular restrictions on values of polymorphic_identity. Regards, - Gulli --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---