[sqlalchemy] Re: all

2012-07-11 Thread Gunnlaugur Briem
Hi burgiduroy, On Wednesday, 11 July 2012 15:24:59 UTC, burgiduroy wrote: Are there any performance difference between the two? for row in query_object.all(): do_something() AND for row in query_object: do_something() The first fetches and processes all rows in one go before

[sqlalchemy] Transplanting _order_by_clause when wrapping into subquery

2012-05-31 Thread Gunnlaugur Briem
Hi, When wrapping a query with an enclosing query to add columns computed from the original query's columns, I'm blatantly doing this: order_clause_list = ClauseList(*fact_query._order_by_clause) fact_query._order_by_clause = ClauseList() subq =

[sqlalchemy] Re: Understanding sqlalchemy memory usage and releasing unused memory

2012-02-07 Thread Gunnlaugur Briem
Hi Manav, the final question did touch SQLAlchemy: the one about expiring and expunging. 1. expiring an object creates a weak reference - this is inaccurate. All object references held by the the session (except for objects whose addition, deletion or attribute change is not yet flushed) are

[sqlalchemy] Re: Building hierarchy tree in reverse

2011-08-01 Thread Gunnlaugur Briem
You could look for recursive CTE (Common Table Expressions), if your database engine supports such queries. See e.g. http://www.postgresql.org/docs/8.4/static/queries-with.html for PostgreSQL. That allows arbitrary-depth queries, as opposed to join chains that have to assume a fixed depth. You

[sqlalchemy] Re: Unable to update Postgres because of natural primary key

2011-07-27 Thread Gunnlaugur Briem
Hi, instead of db.session.add, what you want is: import = db.session.merge(import) See http://www.sqlalchemy.org/docs/orm/session.html#merging : It examines the primary key of the instance. If it’s present, it attempts to load an instance with that primary key (or pulls from the local

Re: RE: [sqlalchemy] Updating records in table not working

2011-07-27 Thread Gunnlaugur Briem
On Wednesday, 27 July 2011 08:23:14 UTC, Simon King wrote: I've looked at the SA documentation and as far as I can see the 'add' does an insert or an update. I think this is incorrect - 'add' always corresponds to 'INSERT' Only for brand new instances, not associated with a session. For

[sqlalchemy] Re: order_by with property of related table

2011-07-27 Thread Gunnlaugur Briem
Hi, you need to join explicitly on A.b: SESSION.query(A).join(A.b).order_by(B.name) Full example: http://pastebin.com/uMqEa6Cr Regards, - Gulli -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit

[sqlalchemy] Re: Constructor and destructor of mapped objects

2011-04-26 Thread Gunnlaugur Briem
Hi Dmitry. You don't need to call the Base constructor, it's just a convenience feature: http://www.sqlalchemy.org/docs/orm/extensions/declarative.html#class-constructor For doing stuff on delete, check out the ORM event API, specifically the before_delete and after_delete events:

[sqlalchemy] Re: Best way to insert different string to Unicode columns?

2011-04-26 Thread Gunnlaugur Briem
Hi, the proper way is to handle character encoding yourself, and pass unicode objects to methods that expect them. When you get this SAWarning, you've just passed in a byte sequence (str in python 2.x) where a unicode object was expected. Your byte sequence *may* be an ASCII-encoded string

[sqlalchemy] Re: unicode everywhere

2011-04-26 Thread Gunnlaugur Briem
Hi Chris, Use Unicode/UnicodeText wherever you have text (and are able to know its character encoding at storage time, preferably always). Text and String are really just byte sequences, and should be used if it's arbitrary bytes you want to store. If you don't have control of the DB schema,

[sqlalchemy] Re: How to add alias for concatentated column ?

2011-02-11 Thread Gunnlaugur Briem
Hi, you mean a label? Just use parentheses: sql = Query([ (Trademark.name + + Model.name).label(combined_name), Model.id.label(id) ]).filter(Model.trademark_id==Trademark.id) HTH, - Gulli -- You received this message because you are subscribed to the Google Groups sqlalchemy

[sqlalchemy] Re: Map Model to Dynamic Select Statement

2011-02-11 Thread Gunnlaugur Briem
Hi, 1. Probably doable by generating the mapped class on the fly, if you insist, but I don't see a reason to. 2. I would certainly rather do it with a function (which you can make a class method if you want to, sure). This is a normal app-level query, after all. - Gulli -- You received

[sqlalchemy] Re: SELECT ARRAY(SELECT ...

2010-11-09 Thread Gunnlaugur Briem
This works: select([ func.ARRAY( select([t.c.value]) .where(t.c.id3) .as_scalar() ) .label('array_col') ]) - Gulli On Nov 9, 3:43 pm, Michael Hipp mich...@hipp.com wrote: Can someone show me the gist of how to construct an SA query that produces SQL* of the form SELECT

[sqlalchemy] Re: Creating tables in correct order

2010-11-08 Thread Gunnlaugur Briem
Hi, that example code works for me in PostgreSQL, after adding unique=True on the name attribute of User, and reversing the order of the drop calls. I don't have a MySQL to try against. Did you get the exact same error from it when running against MySQL? As for your actual app: 1. the log

[sqlalchemy] Re: Creating tables in correct order

2010-11-04 Thread Gunnlaugur Briem
Strictly that's not a query, it's a table definition. Do you mean that you are creating a declarative model class corresponding to this table definition? Posting your code would help more. You must define both tables on the same metadata instance (in declarative, that's typically done by having

[sqlalchemy] Re: hierarchical data storage and searching

2010-09-08 Thread Gunnlaugur Briem
Hi Chris, this is more of a relational design question than SQLAlchemy-related, but take a look at this for an at-a-glance summary of different approaches and their pros and cons: http://vadimtropashko.wordpress.com/2008/08/09/one-more-nested-intervals-vs-adjacency-list-comparison/ ... here for

[sqlalchemy] Couple of breaking changes in 0.6

2010-06-04 Thread Gunnlaugur Briem
Hi, I'm finally adapting our codebase to SqlAlchemy 0.6, and I've found a couple of breaking changes that are not documented in http://www.sqlalchemy.org/trac/wiki/06Migration First, ResultProxy.keys is now a method returning a list, not a list- valued attribute, so expressions like k in

[sqlalchemy] Declarative with mix-in and __table__ fails [patch]

2010-06-04 Thread Gunnlaugur Briem
Hi, if I declaratively map a class with a mix-in and with a __table__ definition that contains the columns in the mix-in, like so: class MyMixin(object): id = Column(Integer, primary_key=True) def foo(self): return 'bar'+str(self.id) class MyModel(Base,MyMixin): __table__ =

[sqlalchemy] Re: Declarative with mix-in and __table__ fails [patch]

2010-06-04 Thread Gunnlaugur Briem
On Jun 4, 9:23 pm, Michael Bayer mike...@zzzcomputing.com wrote: as long as all tests pass it is fine.  If you could give me a patch that includes a test for this in test_declarative, that would be supremely helpful (if you want to make a trac ticket and target it at the 0.6.2 milestone).

[sqlalchemy] Literal table expression (VALUES clause) in select

2010-02-26 Thread Gunnlaugur Briem
Hi, I want to perform a weighted sum over one column, specifying the weights in a literal table (a VALUES clause): select a, sum(b*weights.column1) FROM (select a, b from whatever) foo JOIN (values (1, 0.5), (2, -0.5) ) weights ON weights.column2=a GROUP BY 1; This is on PostgreSQL 8.4.

[sqlalchemy] Re: Literal table expression (VALUES clause) in select

2010-02-26 Thread Gunnlaugur Briem
('weights') Is this more portable, and thus should be used in preference to VALUES as a literal table clause? Regards, - Gulli On Feb 26, 10:28 am, Gunnlaugur Briem gunnlau...@gmail.com wrote: Hi, I want to perform a weighted sum over one column, specifying the weights in a literal table

[sqlalchemy] Re: telling merge() to insert certain objects instead of update

2010-02-08 Thread Gunnlaugur Briem
Trunk commits over the past year: svn log -q --xml --with-revprop svn:author -r {2009-02-09}:HEAD | grep 'author' | sed 's#author\(.*\)/author#\1#' | sort | uniq -c | sort -n -k 1 1 ellisj 1 ram 4 ants 5 jek 14 lele 23 ged 28 empty 39 pjenvey 413 zzzeek So, mostly Mike

[sqlalchemy] Re: MPTT

2010-01-31 Thread Gunnlaugur Briem
that *don't* filter on this property will be faster (single- table query vs. join) if you make the property deferred. Regards, - Gulli On Jan 31, 12:13 am, Gunnlaugur Briem gunnlau...@gmail.com wrote: Yes, you can map a class against an arbitrary select, not just a table, see: http

[sqlalchemy] Re: How to diagnose a transaction hang problem?

2010-01-30 Thread Gunnlaugur Briem
You probably don't have to add locks in your code. The database system takes locks on behalf of your code (in operations initiated by your code) as necessary to ensure transactional consistency. That's one of the benefits of an RDBMS. What you do have to do is to make sure that those database

[sqlalchemy] Re: MPTT

2010-01-30 Thread Gunnlaugur Briem
perhaps as a python @property name level and make it query-able? Like passing it to a filter clause e.g. nested_category.query.filter(nested_category.c.level=='3') On Sat, Jan 30, 2010 at 8:48 AM, Gunnlaugur Briem gunnlau...@gmail.comwrote: Hi Juan, this will do it in version 10.5.8

[sqlalchemy] Re: OperationalError: (OperationalError) no such table:

2010-01-29 Thread Gunnlaugur Briem
Hi James, it would be helpful if you posted the call stack where that error occurs, and the code leading up to the failed query execution, which database and driver (the first word in the engine URL). Without further details, the first place I would look is where the session (or connection) gets

[sqlalchemy] Re: MPTT

2010-01-29 Thread Gunnlaugur Briem
Hi Juan, this will do it in version 10.5.8 (and probably earlier: nested_category = Table( 'nested_category', MetaData(), Column('category_id', Integer, primary_key=True), Column('name', Text, nullable=False), Column('lft', Integer, nullable=False), Column('rgt', Integer,

[sqlalchemy] Re: How to diagnose a transaction hang problem?

2010-01-29 Thread Gunnlaugur Briem
Another quick way of troubleshooting hangs is the tool pg_top, in which you might see a process in the state “Idle in transaction”. This state means that some database operations have been performed in a transaction on that connection but the transaction has not yet been committed. Those database

[sqlalchemy] Re: How to query data from a relational table according to a list of matched conditions?

2009-12-23 Thread Gunnlaugur Briem
You can put the func.count() label in a variable and filter on it: articlecount = func.count(Article.id).label('count') for article in session.query(Article, articlecount) \ .join(Article.keywords).group_by(Article.name) \ .filter(Keyword.name.in_(['k2', 'k3', 'k6',

[sqlalchemy] Warning when relation on mapper supercedes the same relation on inherited mapper

2009-12-16 Thread Gunnlaugur Briem
Hi, I created a mapper inheriting from another mapper and overriding a relation definition, and got this warning: Warning: relation 'dimensions' on mapper 'Mapper|DataSetSlice| dataset_slice' supercedes the same relation on inherited mapper 'Mapper|DataSet|dataset'; this can cause dependency

[sqlalchemy] Re: Warning when relation on mapper supercedes the same relation on inherited mapper

2009-12-16 Thread Gunnlaugur Briem
On Dec 16, 4:08 pm, Michael Bayer mike...@zzzcomputing.com wrote: - there's two tables, dataset_table and datasetslice_table. - these two tables have *two* foreign key references to each other - an inheritance relationship (on unknown columns since they aren't displayed here) and another on

[sqlalchemy] Hyper-eager autoflush for debugging

2009-11-16 Thread Gunnlaugur Briem
Hi, a common pattern for me is to encounter a ProgrammingError or other exception on autoflush. Then the point in my code at which I made a booboo has already passed and so is not indicated by the stack trace. If the cause isn't obvious, I will go rerunning the failing test, inserting

[sqlalchemy] Re: Hyper-eager autoflush for debugging

2009-11-16 Thread Gunnlaugur Briem
On Nov 16, 3:38 pm, Michael Bayer mike...@zzzcomputing.com wrote: id like to know what errors specifically you get which aren't fairly obvious even if they happen later on.   I'm guessing some of the dependency oriented messages like dependency rule tried to blank out primary key. Nah, the

[sqlalchemy] Re: Can I coerce strings into Unicode?

2009-06-12 Thread Gunnlaugur Briem
The engine's conversion to unicode doesn't happen when you assign the property, it happens when the underlying database operation is committed, and arrives in the python object's property only after roundtripping through the database. In [50]: m1.body = 'new - NOT unicode' In [51]: m1.body

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

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

[sqlalchemy] Re: Using sqlalchemy in twisted.

2009-03-04 Thread Gunnlaugur Briem
Hi 一首诗, what database engine are you using? On PostgreSQL at least, table creation and dropping (and some other operations) take an ACCESS EXCLUSIVE lock on the database, and will wait for this lock indefinitely if there are open transactions hanging around. My app creates and drops tables

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

2009-03-02 Thread Gunnlaugur Briem
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,

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

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

[sqlalchemy] with_statement support in session objects

2009-02-24 Thread Gunnlaugur Briem
Hi, trying to be careful to close all sessions when I'm done with them, I find myself doing this all the time: session = Session() try: do_stuff_with(session) finally: session.close() This would be neater: with Session() as session: do_stuff_with(session) but the

[sqlalchemy] Typo in synonym documentation?

2009-02-03 Thread Gunnlaugur Briem
Hi, in the docs, here: http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#defining-synonyms there is this code snippet: class MyClass(Base): __tablename__ = 'sometable' _attr = Column('attr', String) def _get_attr(self): return self._some_attr def