Re: [sqlalchemy] Re: trouble with metaclass

2011-03-21 Thread Chris Withers

On 21/03/2011 19:29, farcat wrote:

Hi Chris,

The short answer is that I want to dynamically create classes/tables


Why?


and I want to implement multiple inheritance


That's fine, that's what mixins give you.
If you have more than one class with a __tablename__ in the class 
hierarchy, then you'll get table inheritance, which may also be what you 
want.



(simplified version: just
data and no overrides =>


What do you mean by "overrides"?


 no diamond problem),


What do you mean by "diamond problem"? Python has a well defined MRO, if 
that's what you're worried about...



so I need some extra
work like a extra tables for polymorphism.


What does polymorphism mean to you? SQLAlchemy handles most polymorphic 
OR mapping quite gracefully...



I think it would be most
elegant to do this with metaclasses, but I am just experimenting until
now.


You come across as someone with a hammer desperately looking for nails 
and frustrated when you find only screws ;-)


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
   - http://www.simplistix.co.uk

--
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.



Re: [sqlalchemy] Relationship operator vs join and filter

2011-03-21 Thread Michael Bayer

On Mar 21, 2011, at 7:21 AM, Wouter Overmeire wrote:

> New to sql and sqlalchemy.
> Using an in memory sqlite database.
> 
> Trying to do some queries based on the examples in the sqlalchemy
> documentation.
> 
> I have classes: Tag, Article, Author, Award
> article <--> tag: many to many
> article <--> author: many to many (so one article can have multiple
> authors)
> award <--> author: many to many
> 
> Find all articles written by 'author1'
> articles =
> session.query(Article).filter(Article.authors.any(Author.name ==
> 'author1')).all()
> This works fine, until i start increasing the number of articles and
> authors.
> For 50 authors with each 1000 articles and each article has 10 tags
> (out of 100 tags in total), the above query takes forever, i never let
> it complete and interrupt it after 15 minutes. The above query is just
> as in the documentation (http://www.sqlalchemy.org/docs/orm/
> tutorial.html?highlight=blogpost#building-a-many-to-many-
> relationship), is it suppose to work on larger tables? Very likely i`m
> doing something wrong.

Not really, the EXISTS format is known to be generally slower than a straight 
INNER JOIN.   The operator is convenient, especially when doing a NOT EXISTS, 
but if looking for positive inclusion of sets the more verbose join() approach 
is typically more efficient.

> 
> If i do:
> articles =
> session.query(Article).join(Article.authors).filter(Author.name ==
> 'author1').all()  this is done in a fraction of a second.
> 
> Now trying to find articles written by 'author1' and having tag
> 'tag1'.
> qArticleByTag =
> session.query(Article).join(Article.tags).filter(Tag.tag == 'tag1')
> qArticleByAuthor =
> session.query(Article).join(Article.authors).filter(Author.name ==
> 'author1')
> articles = qArticleByTag.intersect(qArticleByAuthor).all()
> 
> This works fine, but somehow i have the feeling there are more
> efficient ways.

the SQL set operators like UNION, INTERSECT, etc. are not the most efficient 
and are cumbersome to use.  Using two joins() at once would be more 
straightforward:

session.query(Article).join(Article.tags).filter(Tag.tag=='tag1').join(Article.authors).filter(Author.name=='author1')

three sets of rows are intersected above, returning only those rows which are 
correlated and meet all criterion.

> Since i think the above query goes twice through all
> the articles.

with INTERSECT, probably, though its possible some query optimizers could 
figure out a more efficient pattern here (I'm not a deep expert in query 
optimizers to know for sure, however).  Sticking with chains of joins is the 
way to go in any case.


-- 
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: trouble with metaclass

2011-03-21 Thread farcat
Hi Chris,

The short answer is that I want to dynamically create classes/tables
and I want to implement multiple inheritance (simplified version: just
data and no overrides => no diamond problem), so I need some extra
work like a extra tables for polymorphism. I think it would be most
elegant to do this with metaclasses, but I am just experimenting until
now. I will look at the mixin classes chapter though; I have some more
challenges ... The long answer (when i get it to work) might come
later.

Cheers, Lars

On Mar 20, 8:33 pm, Chris Withers  wrote:
> On 19/03/2011 10:27, farcat wrote:
>
> > OK, dumb error, move Base.metadata.create_all(engine)  below table1 =
> > tablemeta("table1")  and it works.
>
> > This works when I create the columns via the temp dict in __new__ not
> > when I do so in __init__ (it complains about missing __tabledata__),
> > but why?
>
> I'd strongly suggest reading the section on Mixin classes rather than
> doing stuff with Metaclasses:
>
> http://www.sqlalchemy.org/docs/orm/extensions/declarative.html?highli...
>
> If you *still* feel you need to use metaclasses after that, please let
> us know why! ;-)
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>             -http://www.simplistix.co.uk

-- 
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.



Re: [sqlalchemy] Pypi release policy

2011-03-21 Thread Michael Bayer

On Feb 14, 2011, at 5:22 AM, Tarek Ziadé wrote:

>userid = Column(Integer(11), primary_key=True, nullable=False)
> TypeError: __init__() takes exactly 1 argument (2 given)
> 
> 
> The code: 
> http://hg.mozilla.org/services/server-storage/file/78762deede5d/syncstorage/storage/sqlmappers.py#l55

I didn't expect that anyone was passing arguments to types like Integer, the 
value you pass there is meaningless as the default Integer doesn't have any 
kind of length. 

I will restore the "catchall" constructor to the base type class with a 
deprecation warning.

-- 
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: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-21 Thread ddarko
Everything is clear - so wait for 0.7 finall.
Meanwhile, would use pg8000. Pity that does not support pgarray :/
thx

On Mar 21, 3:31 pm, Michael Bayer  wrote:
> Yeah psycopg2 with python 3 / sqlalchemy is not supported yet.  The python 3 
> supported version of psycopg2 came out like, in the past two weeks.    It 
> will be 0.7 where its supported at all, hopefully soon as its a matter of 
> setting up some flags in the dialects.     In the meantime if you want to 
> start working with pg + SQLA + py3k you can try the python 3 version of 
> pg8000 - its not as nice or anywhere near as fast as psycopg2 but it works in 
> at least a rudimentary fashion.
>
> Also Python 3.2 is the latest version of py3k and actually has some fairly 
> dramatic behavioral differences vs. 3.1.   If you want to work with 3.2 and 
> let us know what quirks you find...
>
> On Mar 21, 2011, at 10:02 AM, ddarko wrote:
>
>
>
>
>
>
>
> > Python 3.1
> > SQLAlchemy 0.6.6  (and tested with 0.7b3)
> > psycopg2 2.4
> > postgresql 8.4
>
> > Has anyone tested this configuration?
>
> > psycopg2 alone work for me great, but in spite of installing the
> > current version of SQLAlchemy accordance with the instructions
> > README.py3k connection is not working properly:
>
> > print(db.execute("select count(*) from news;").fetchall())
>
> > $ python _test_sqlalchemy.py
> > Traceback (most recent call last):
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 671, in _do_get
> >    return self._pool.get(wait, self._timeout)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/util/queue.py", line 137, in get
> >    raise Empty
> > sqlalchemy.util.queue.Empty
>
> > During handling of the above exception, another exception occurred:
>
> > Traceback (most recent call last):
> >  File "_test_sqlalchemy.py", line 12, in 
> >    print(db.execute("select count(*) from news;").fetchall())
> >  File "/home//lib/db_model.py", line 28, in execute
> >    return dbs.execute(a)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/orm/scoping.py", line 113, in do
> >    return getattr(self.registry(), name)(*args, **kwargs)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/orm/session.py", line 758, in execute
> >    return self._connection_for_bind(bind,
> > close_with_result=True).execute(
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/orm/session.py", line 694, in
> > _connection_for_bind
> >    return self.transaction._connection_for_bind(engine)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/orm/session.py", line 246, in
> > _connection_for_bind
> >    conn = bind.contextual_connect()
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/engine/base.py", line 2062, in contextual_connect
> >    self.pool.connect(),
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 208, in connect
> >    return _ConnectionFairy(self).checkout()
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 369, in __init__
> >    rec = self._connection_record = pool._do_get()
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 693, in _do_get
> >    con = self._create_connection()
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 173, in _create_connection
> >    return _ConnectionRecord(self)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/pool.py", line 257, in __init__
> >    pool.dispatch.first_connect.exec_once(self.connection, self)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/event.py", line 227, in exec_once
> >    self(*args, **kw)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/event.py", line 236, in __call__
> >    fn(*args, **kw)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/engine/strategies.py", line 162, in first_connect
> >    dialect.initialize(c)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 793, in
> > initialize
> >    super(PGDialect, self).initialize(connection)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/engine/default.py", line 171, in initialize
> >    self._get_server_version_info(connection)
> >  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> > py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 964, in
> > _get_server_version_info
> >    v = 

Re: [sqlalchemy] Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-21 Thread Michael Bayer
Yeah psycopg2 with python 3 / sqlalchemy is not supported yet.  The python 3 
supported version of psycopg2 came out like, in the past two weeks.It will 
be 0.7 where its supported at all, hopefully soon as its a matter of setting up 
some flags in the dialects. In the meantime if you want to start working 
with pg + SQLA + py3k you can try the python 3 version of pg8000 - its not as 
nice or anywhere near as fast as psycopg2 but it works in at least a 
rudimentary fashion.

Also Python 3.2 is the latest version of py3k and actually has some fairly 
dramatic behavioral differences vs. 3.1.   If you want to work with 3.2 and let 
us know what quirks you find...


On Mar 21, 2011, at 10:02 AM, ddarko wrote:

> Python 3.1
> SQLAlchemy 0.6.6  (and tested with 0.7b3)
> psycopg2 2.4
> postgresql 8.4
> 
> Has anyone tested this configuration?
> 
> psycopg2 alone work for me great, but in spite of installing the
> current version of SQLAlchemy accordance with the instructions
> README.py3k connection is not working properly:
> 
> print(db.execute("select count(*) from news;").fetchall())
> 
> 
> $ python _test_sqlalchemy.py
> Traceback (most recent call last):
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 671, in _do_get
>return self._pool.get(wait, self._timeout)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/util/queue.py", line 137, in get
>raise Empty
> sqlalchemy.util.queue.Empty
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
>  File "_test_sqlalchemy.py", line 12, in 
>print(db.execute("select count(*) from news;").fetchall())
>  File "/home//lib/db_model.py", line 28, in execute
>return dbs.execute(a)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/orm/scoping.py", line 113, in do
>return getattr(self.registry(), name)(*args, **kwargs)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/orm/session.py", line 758, in execute
>return self._connection_for_bind(bind,
> close_with_result=True).execute(
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/orm/session.py", line 694, in
> _connection_for_bind
>return self.transaction._connection_for_bind(engine)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/orm/session.py", line 246, in
> _connection_for_bind
>conn = bind.contextual_connect()
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/engine/base.py", line 2062, in contextual_connect
>self.pool.connect(),
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 208, in connect
>return _ConnectionFairy(self).checkout()
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 369, in __init__
>rec = self._connection_record = pool._do_get()
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 693, in _do_get
>con = self._create_connection()
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 173, in _create_connection
>return _ConnectionRecord(self)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/pool.py", line 257, in __init__
>pool.dispatch.first_connect.exec_once(self.connection, self)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/event.py", line 227, in exec_once
>self(*args, **kw)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/event.py", line 236, in __call__
>fn(*args, **kw)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/engine/strategies.py", line 162, in first_connect
>dialect.initialize(c)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 793, in
> initialize
>super(PGDialect, self).initialize(connection)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/engine/default.py", line 171, in initialize
>self._get_server_version_info(connection)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 964, in
> _get_server_version_info
>v = connection.execute("select version()").scalar()
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/engine/base.py", line 1259, in execute
>params)
>  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
> py3.1.egg/sqlalchemy/engine/base.py", line 1436, in _execute_text
>stateme

[sqlalchemy] Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-21 Thread ddarko
Python 3.1
SQLAlchemy 0.6.6  (and tested with 0.7b3)
psycopg2 2.4
postgresql 8.4

Has anyone tested this configuration?

psycopg2 alone work for me great, but in spite of installing the
current version of SQLAlchemy accordance with the instructions
README.py3k connection is not working properly:

print(db.execute("select count(*) from news;").fetchall())


$ python _test_sqlalchemy.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 671, in _do_get
return self._pool.get(wait, self._timeout)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/util/queue.py", line 137, in get
raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "_test_sqlalchemy.py", line 12, in 
print(db.execute("select count(*) from news;").fetchall())
  File "/home//lib/db_model.py", line 28, in execute
return dbs.execute(a)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/orm/scoping.py", line 113, in do
return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/orm/session.py", line 758, in execute
return self._connection_for_bind(bind,
close_with_result=True).execute(
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/orm/session.py", line 694, in
_connection_for_bind
return self.transaction._connection_for_bind(engine)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/orm/session.py", line 246, in
_connection_for_bind
conn = bind.contextual_connect()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 2062, in contextual_connect
self.pool.connect(),
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 208, in connect
return _ConnectionFairy(self).checkout()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 369, in __init__
rec = self._connection_record = pool._do_get()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 693, in _do_get
con = self._create_connection()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 173, in _create_connection
return _ConnectionRecord(self)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/pool.py", line 257, in __init__
pool.dispatch.first_connect.exec_once(self.connection, self)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/event.py", line 227, in exec_once
self(*args, **kw)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/event.py", line 236, in __call__
fn(*args, **kw)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/strategies.py", line 162, in first_connect
dialect.initialize(c)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 793, in
initialize
super(PGDialect, self).initialize(connection)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/default.py", line 171, in initialize
self._get_server_version_info(connection)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/dialects/postgresql/base.py", line 964, in
_get_server_version_info
v = connection.execute("select version()").scalar()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 1259, in execute
params)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 1436, in _execute_text
statement, parameters
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 1519, in _execute_context
result = context.get_result_proxy()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/dialects/postgresql/psycopg2.py", line 198, in
get_result_proxy
return base.ResultProxy(self)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 2420, in __init__
self._init_metadata()
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 2427, in _init_metadata
self._metadata = ResultMetaData(self, metadata)
  File "/usr/local/lib/python3.1/dist-packages/SQLAlchemy-0.7b3dev-
py3.1.egg/sqlalchemy/engine/base.py", line 2283, in __init__
colnam

[sqlalchemy] Relationship operator vs join and filter

2011-03-21 Thread Wouter Overmeire
New to sql and sqlalchemy.
Using an in memory sqlite database.

Trying to do some queries based on the examples in the sqlalchemy
documentation.

I have classes: Tag, Article, Author, Award
article <--> tag: many to many
article <--> author: many to many (so one article can have multiple
authors)
award <--> author: many to many

Find all articles written by 'author1'
articles =
session.query(Article).filter(Article.authors.any(Author.name ==
'author1')).all()
This works fine, until i start increasing the number of articles and
authors.
For 50 authors with each 1000 articles and each article has 10 tags
(out of 100 tags in total), the above query takes forever, i never let
it complete and interrupt it after 15 minutes. The above query is just
as in the documentation (http://www.sqlalchemy.org/docs/orm/
tutorial.html?highlight=blogpost#building-a-many-to-many-
relationship), is it suppose to work on larger tables? Very likely i`m
doing something wrong.

If i do:
articles =
session.query(Article).join(Article.authors).filter(Author.name ==
'author1').all()  this is done in a fraction of a second.

Now trying to find articles written by 'author1' and having tag
'tag1'.
qArticleByTag =
session.query(Article).join(Article.tags).filter(Tag.tag == 'tag1')
qArticleByAuthor =
session.query(Article).join(Article.authors).filter(Author.name ==
'author1')
articles = qArticleByTag.intersect(qArticleByAuthor).all()

This works fine, but somehow i have the feeling there are more
efficient ways. Since i think the above query goes twice through all
the articles. How do i search for articles written by author one in
the articles i have selected by tag1? I tried what`s in the
documentation, and that works fine for small number of articles but
not when i have like 50k articles.

I haven`t found yet a fast way to query for articles with tag 'tag1'
written by any author who has won award 'award1'.

If anyone can help, very much appreciated.

Thanks.



-- 
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.



Re: [sqlalchemy] In-memory object duplication

2011-03-21 Thread Jacques Naude
Hi, Simon

You nailed it! Thanks a million for the guidance and suggestion - it worked
like a bomb. I simply removed the "offending" append statement and my double
entries have disappeared, end the entry still persists to the database. (It
seems so simple and obvious with the benefit of insight and hindsight,
doesn't it?)

Once again, thanks.

Greetings

On 17 March 2011 17:13, King Simon-NFHD78
wrote:

> > -Original Message-
> > From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
> > On Behalf Of Jacques Naude
> > Sent: 17 March 2011 12:32
> > To: sqlalchemy@googlegroups.com
> > Subject: Re: [sqlalchemy] In-memory object duplication
> >
> > Hi, Simon
> >
> > Thanks for the quick response.
> >
> > Elixir doesn't use __init__ - there's something automatic going on
> > there. My create(), in essence, does the job of __init__, which means
> > you might still be hitting the nail on the head. I haven't had the
> > time to test it out yet, but I will. (Why, though, would the double
> > entry not be persisted to the database too?)
> >
>
> The entry only appears once in the database because SQAlchemy works hard
> to ensure that a single object instance corresponds to a single row in
> the database. It doesn't really make sense (in the standard one-to-many
> model) for a particular child to appear more than once in a parent-child
> relationship.
>
> By default, SA uses a list as the collection implementation for
> relationships, and doesn't care if you add the same instance more than
> once. If it bothers you, you could use a set instead:
>
> http://www.sqlalchemy.org/docs/orm/collections.html#customizing-collecti
> on-access
>
> Simon
>
> --
> 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.
>
>

-- 
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.