Re: [sqlalchemy] Strange issue with unicode

2014-07-23 Thread Gunnlaugur Thor Briem
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,

Re: [sqlalchemy] dogpile with SA inheritance

2014-04-15 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] dogpile with SA inheritance

2014-04-15 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] Is it possible to create a filter 'string' LIKE column + '%'?

2014-04-10 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] Could anyone please help explain expired object in plain language?

2014-02-23 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] Is there a way to examine how many objects a session currently contains?

2014-02-19 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] ProgrammingError: cannot determine type of empty array even with proper model declaration?

2013-12-03 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] PG's JSON Data Type

2013-11-18 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] many queries select if in cycle has insert into table

2013-08-30 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] sqlite string concats and datetime arithmetics

2013-08-30 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] sqlite string concats and datetime arithmetics

2013-08-29 Thread Gunnlaugur Thor Briem
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'))

Re: [sqlalchemy] Calculate birthdays

2013-08-28 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] Calculate birthdays

2013-08-28 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] can a SqlAlchemy session be extracted from an object ?

2013-05-02 Thread Gunnlaugur Thor Briem
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

Re: [sqlalchemy] [Q] WindowedRangeQuery recipe

2013-04-26 Thread Gunnlaugur Thor Briem
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

[sqlalchemy] Re: Late and ugly error when mixing timezone-savvy and timezone-naive datetimes

2009-03-12 Thread Gunnlaugur Thor Briem
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:

[sqlalchemy] Re: Why do I have to begin a transaction?

2009-03-04 Thread Gunnlaugur Thor Briem
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

[sqlalchemy] Re: Infinite recursion in sqlalchemy/orm/attributes.py when running under debugger

2009-03-03 Thread Gunnlaugur Thor Briem
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:

[sqlalchemy] Re: Creating SQL Expression

2009-02-26 Thread Gunnlaugur Thor Briem
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

[sqlalchemy] Re: Ordering results of a WHERE x in y query by y

2009-02-26 Thread Gunnlaugur Thor Briem
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

[sqlalchemy] Re: Creating SQL Expression

2009-02-25 Thread Gunnlaugur Thor Briem
)) 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

[sqlalchemy] Re: newbie problem

2009-02-18 Thread Gunnlaugur Thor Briem
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

[sqlalchemy] polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread Gunnlaugur Thor Briem
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