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
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 =
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
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
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
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
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
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:
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
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,
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
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
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
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
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
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
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
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__ =
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).
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.
('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
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
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
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
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
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
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,
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
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',
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
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
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
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
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
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
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
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,
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?
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
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
40 matches
Mail list logo