[sqlalchemy] count(*) function

2007-01-15 Thread milena


Hi,

does anyone know the syntax of

SELECT COUNT(*) FROM table_name;

for SQLAlchemy? Is there a list of functions (used in SELECT statement)
that exist in SQL that I can use in SQLAlchemy? btw, I am using MySQL.

I need to see if my table is empty (isemty function doesn't work).

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: count(*) function

2007-01-15 Thread Marco Mariani


milena wrote:


I have tried

select([func.count(*)], from_obj=[table_name]).execute()

but it didn't work

I suppore you're not using mappers, so this is the fastest method:


number_of_rows = table.count().execute().fetchone()[0]

where table is the table object


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: count(*) function

2007-01-15 Thread Sébastien LELONG



I have tried

select([func.count(*)], from_obj=[table_name]).execute()

but it didn't work


I think you should try to specify a column in your count or leave it empty 
(didn't try). If you're using mapped objects, you can use the SelectResults 
extension:


from sqlalchemy.ext.selectresults import SelectResults
SelectResults(your_session.query(YourMappedClass)).count()


Cheers,

Seb
--
Sébastien LELONG
sebastien.lelong[at]sirloon.net

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Column aliases

2007-01-15 Thread Marco Mariani


Michael Bayer wrote:

to have aliases of properties that are usable with get_by(), use the
synonym function, described in:

http://www.sqlalchemy.org/docs/adv_datamapping.myt#advdatamapping_properties_overriding
  


Thank you. Altough the docs are very well done, there are many APIs and
that sometimes may be confusing.



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Questions about polymorphic mappers

2007-01-15 Thread King Simon-NFHD78


Micheal Bayer wrote:


id rather just add another plugin point on MapperExtension 
for this, which takes place before the polymorphic decision 
stage at the top of the _instance method, like 
get_polymorphic_identity().  that way you could do all of 
this stuff cleanly in an extension (and id do that instead of 
making polymorphic_identity into a list).  hows that sound?




That would be ideal for me, and would seem to be the most flexible
solution as well - it leaves the decision for which class to use up to
the application. What would it actually return, though? An instance
ready to be populated?

Thanks a lot,

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: pickle and mapped objects

2007-01-15 Thread Manlio Perillo


Michael Bayer ha scritto:


if you can pickle the object by itself, then id guess youre using lazy
loaders with the query.options(lazyload('something')) call, which
places per-object lazy loaders (i.e. callables) on your objects.


I'm not sure, since I never use lazy loaders (I use Twisted).


either make sure the lazy loaders are fired off, or make lazyload the
default loading for that mapper.  similar advice if you are using
deferred columns.



The problem can be with a nested relation (always lazy loaded, as I can 
understand).


However the best solution is to implement __setstate__, also for 
avoiding trash in the database.



Thanks and regards  Manlio Perillo

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: adding a foreign key constraint to an existing table

2007-01-15 Thread Jonathan Ellis


On 1/15/07, Michael Bayer [EMAIL PROTECTED] wrote:

2. no support for CASCADE in the DROP statement right now.  someone
fill me in, is DROP CASCADE part of the sql standard ?


yes, at least for tables.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] portable schema

2007-01-15 Thread Manlio Perillo


Hi.

I don't know if this already implemented or planned.

Schemas are supported by SQLAlchemy, but unfortunately not all database 
supported by SQLAlchemy supports schema.


My idea is simple.
1) When the database does not support schema, the schema name specified
   in Table costructor should be prepended to the table name.

   foo = Table('foo', ..., schema='bar')
   result in a table named bar_foo

2) If the database is SQLite, the schema name should be used to create a
   new database, attaching it to the main database.

   engine = create_engine('sqlite://db')
   foo = Table('foo', ..., schema='bar')

   result in a new database named db_bar.

   Of course the user can decide if to use solution 1)




Regards  Manlio Perillo



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: portable schema

2007-01-15 Thread Michael Bayer



On Jan 15, 2007, at 12:05 PM, Manlio Perillo wrote:


My idea is simple.
1) When the database does not support schema, the schema name  
specified

   in Table costructor should be prepended to the table name.

   foo = Table('foo', ..., schema='bar')
   result in a table named bar_foo



why not just name your table bar_foo ?  why have two ways to do the  
same thing ?  how would this work with autoloading ?  why make a  
keyword argument called schema do something that is nothing like a  
what a schema actually is ?


2) If the database is SQLite, the schema name should be used to  
create a

   new database, attaching it to the main database.

   engine = create_engine('sqlite://db')
   foo = Table('foo', ..., schema='bar')

   result in a new database named db_bar.


why not just conenct to a database file called db_bar?  why have  
two ways to do the same thing ? why make a keyword argument called  
schema do something that is nothing like a what a schema actually  
is ?






--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Questions about polymorphic mappers

2007-01-15 Thread Michael Bayer


at this point the entity_name should get set after your custom  
create_instance is called (at least thats in the trunk).  init_attr  
is not required, it pre-sets attributes on the object that are  
otherwise auto-created later (but the autocreation step throws a  
single AttributeError per attribute, which hits performance a little  
bit).



On Jan 15, 2007, at 12:19 PM, King Simon-NFHD78 wrote:



Michael Bayer wrote:
you can still override create_instance() as well and try to spit  
out subclasses that are otherwise not mapped.


This was something I looked at a while ago as well, and I wasn't sure
what the requirements on objects returned from create_instance  
were. If

it is not overridden, the mapper calls _create_instance, which sets
_entity_name and calls attribute_manager.init_attr. How important are
these things to the rest of the library?

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Constructing where-clauses dynamically

2007-01-15 Thread Michael Bayer


in continuing
http://groups.google.com/group/sqlalchemy/browse_thread/thread/4dab6cfae1f62521/8b66851007c6a652?lnk=gstq=daniel+millerrnum=2#8b66851007c6a652
, which bizarrely GG will not let me continue, on a closely related but
not exactly the same issue, i broke my limit of people expecting this
particular behavior and just added it:

   # get an instance of Address
   someaddress = session.query(Address).get_by(street='123 Green
Street')

   # look for User instances which have the
   # someaddress instance in their addresses collection
   l = session.query(User).select_by(addresses=someaddress)

so, not quite the same as the previous discussion since we are back in
the XXX_by world of things, but this would be SA's first foray into
higher-level querying functionality.   as ive maintained, i dont think
the Query object can go too far with this since it ultimately would
need some totally reworked querying paradigm to come up with some kind
of OQL.


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Action on object deletion

2007-01-15 Thread aldo



Hi Folks,


I have an application that needs to do a certain amount of housekeeping when
objects are deleted or edited. Essentially, I need to keep historical data, so
under some circumstances deleted objects actually need to be obsoleted, and
edited objects need to be copied and references to them from elsewhere
updated. Now, object modification can be caught and handled in the object
itself without (too much) hassle. But object deletion through a session is
another matter...

My first impulse was to use a mapper extension and before_delete. This is not
very satisfying, though - since before_delete is called inside a flush(), the
ORM can't be used, and manipulating the database straight through the
connection object during a flush() just seems brittle. 


Another option is to put a .delete() method on the mapped object itself, and
mandate that objects should be deleted through this method. This means that
these objects would need to be treated differently througout the codebase,
which is not nice. That said, this is the approach I'm using at the moment.

What would be nice is if a method on an object could be called when
session.delete(object) is called, before the flush() happens. To do this,
Session.delete might be extended to provide MapperExtension-like functionality:
   
   def delete(self, object, entity_name=None):
   ...
   if hasattr(object, on_delete):

   r = object.on_delete(self)
   if r == EXT_PASS:
   return
   ...

The same thing could be done for other session.X operations on mapped objects.
I'm not yet very familiar with SQLAlchemy's internals, so I'm sure there are
subtleties I haven't thought of, but you get the drift.

So, firstly: is there a nicer way to do this? Secondly, is a patch that does
something like the above a good idea?




Cheers,



Aldo


--
Aldo Cortesi
[EMAIL PROTECTED]
http://www.nullcube.com
Mob: 0419 492 863

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---