[sqlalchemy] Re: how to force a clean refresh of a lazy loaded attribute

2007-02-26 Thread Manlio Perillo

Michael Bayer ha scritto:
 
 On Feb 25, 2007, at 9:03 AM, Manlio Perillo wrote:
 
 Michael Bayer ha scritto:
 On Feb 23, 2007, at 1:56 PM, Manlio Perillo wrote:

 Hi again.

 I have an object attribute loaded via lazy loader.
 This object is loaded in a transaction.

 Then, in another transaction, I ussue an update statement (via  
 the sql
 module, not using the orm), that updates the table of the main
 object's
 attribute.

 The problem, now, is that I want to reload this attribute.

 I have tried, in a separate transaction:
 sess.update(obj)
 sess.expire(obj)

 One problem here is that the entire object is reloaded, and I do not
 want this.
 i think if you say delattr(obj, attributename), it will do a lazyload
 on the next run.

 Sorry, I have tested only sess.expire(obj).

 delattr(obj, attributename) does not works.

 
 OK, again, if you want the actual object that is *in* the lazy loaded  
 collection to be reloaded, you have to issue an expire() or refresh()  
 on at least that object.  if the object is already in the session,  
 the lazy load may still fire off but will only reload the instance  
 that is already in the session.
 
 so you might want do instead do this (assuming its a lazy load  
 collection - modify accordingly for a scalar attribute):
 
 for o in myinstance.somecollection:
  sess.expire(o)
 

This works, thanks!

However, for non scalar attribute, I would like to simply do:
sess.expire(myinstance.somecollection)



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] Running initialization code after creating/loading an object

2007-02-26 Thread Sanjay

Hi,

I am not able to code this pattern. I read about it at many places, in
doc, FAQ and at
http://groups.google.co.in/group/sqlalchemy/browse_thread/thread/66d0094c671bd5bc,
but still could not do it. May be my noviceness. Needing help. I tried
somethings like this, but it did not help:

from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
from sqlalchemy.ext.sessioncontext import SessionContext

context = SessionContext(create_session)
session = context.current

metadata = BoundMetaData('sqlite:///satest', echo=False)

# table definitions
person_table = Table('person', metadata,
Column('person_id', Integer, primary_key=True, autoincrement =
True),
Column('first_name', Unicode(30)),
Column('last_name', Unicode(30)))

metadata.drop_all()
metadata.create_all()

class Person(object):

@staticmethod
def Create(first_name, last_name):
p = Person(first_name=first_name, last_name=last_name)
p.CallAfterInitializationOrFetchingFromDatabase()
return p

def CallAfterInitializationOrFetchingFromDatabase(self):
self.full_name = self.first_name + ' ' + self.last_name

class PersonExtension(object):

def create_instance(self, mapper, selectcontext, row, Person):
p = Person()
p.CallAfterInitializationOrFetchingFromDatabas()
return p

assign_mapper(context, Person, person_table,
extension=PersonExtension())

p = Person.Create(first_name=Sanjay, last_name=Patel)
Assert p.full_name == Sanjay Patel
p.flush()
del p
session.clear()
p = Person.get_by(person_id=1)
assert p.full_name == Sanjay Patel

I think using __new__ might be simpler, but can't guess how exactly
to use it.

thanks
Sanjay


--~--~-~--~~~---~--~~
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] How do I tell if an object has been INSERTed or UPDATEd?

2007-02-26 Thread Simon Willison

Hi all,

I've got a bit of code that looks like this:

session = get_session()
session.save(obj)
session.flush()

What's the best way of telling if obj has been newly created (INSERT)
or merely updated (UPDATE)? I tried just checking for obj.id is None
but I can't garauntee that my primary key is called 'id'. Do I need to
introspect the primary key myself or is there a neater solution?

Cheers,

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: How do I tell if an object has been INSERTed or UPDATEd?

2007-02-26 Thread King Simon-NFHD78

Marco Mariani wrote:
 
 Simon Willison wrote:
  I've got a bit of code that looks like this:
 
  session = get_session()
  session.save(obj)
  session.flush()

 You can see what's going to be inserted/updated/deleted by 
 accessing session.new, session.dirty, session.deleted
 
 http://www.sqlalchemy.org/docs/unitofwork.myt
 
  What's the best way of telling if obj has been newly 
 created (INSERT) 
  or merely updated (UPDATE)? I tried just checking for 
 obj.id is None
  but I can't garauntee that my primary key is called 'id'.
 I would hope so! :-))
 

If you are wanting to know _after_ the session.flush(), I don't think
session.new/dirty/deleted will help you. Also, your primary key will be
read back from the database immediately after INSERT, so it won't be
None. Between, the save and the flush, obj in session.new should do
the job.

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: How do I tell if an object has been INSERTed or UPDATEd?

2007-02-26 Thread Simon Willison

On Feb 26, 3:25 pm, King Simon-NFHD78 [EMAIL PROTECTED]
wrote:
 If you are wanting to know _after_ the session.flush(), I don't think
 session.new/dirty/deleted will help you. Also, your primary key will be
 read back from the database immediately after INSERT, so it won't be
 None. Between, the save and the flush, obj in session.new should do
 the job.

Brilliant - that's exactly what I was after. 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: how to get column names in result

2007-02-26 Thread Jonathan Ellis

You can see what columns are part of a table (or a select!) with
.columns.keys() or .c.keys().

On 2/24/07, vkuznet [EMAIL PROTECTED] wrote:

 Hi,
 a very simple question which I cannot find in documentation. How to
 get column names together with result. This is useful for web
 presentation of results using templates. I used use_labels=True,
 indeed it construct query with names, but my final result contains
 only column values. What I wanted is to get
 as a first row the column names.

 Thanks,
 Valentin.


 


--~--~-~--~~~---~--~~
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: Complicated Mapper with Count Function

2007-02-26 Thread Jonathan Ellis

you need to use an outer join (or a subselect) to get a row to come
back even when there is no match in the right table.

adding from_obj=[outerjoin(product_table, stock_product_table)] to
what you have now would probably work.

(personally i think a subselect would be better style here but i can't
predict if it would perform better.  since you are so close to having
it working the other way maybe you should just run with that. :)

On 2/25/07, Koen Bok [EMAIL PROTECTED] wrote:

 Dear all.

 I have to make a complicated mapper, and I need a little help.

 We have a list of products. These products have stock, each individual
 stock item has an entry in the stockproduct table. So to get the total
 stock we need to count the items in the stock database. We can filter
 them by a particular stock.

 So I made a function to create a mapper to do just that. But there are
 two problems:

 - It's not working well, because if the count function equals 0 (no
 stock) the product does not appear in the results.
 - I have the feeling this can be better optimized, but I can't see it
 (maybe put it in a join or subquery?)

 The function

 def productStockMapper(stockList):

 or_list = or_()
 for stock in stockList:
 or_list.append(stockproduct_table.c.stock_id==stock.id)

 s = select([product_table,
 func.count(stockproduct_table.c.id).label('stock')],
 and_(
 stockproduct_table.c.product_id==product_table.c.id,
 or_list),
 group_by=[c for c in product_table.c]).alias('count_select')

 return mapper(Product, s, non_primary=True)

 The tables:

 product_table = Table('products', metadata,
 Column('id', Integer, primary_key=True),
 Column('name', Unicode(255), nullable=False)
 Column('code', Unicode(255), unique=True, nullable=False))

 stockproduct_table = Table('stockproducts', metadata,
 Column('id', Integer, primary_key=True),
 Column('stock_id', Integer, ForeignKey(stocks.id), nullable=False),
 Column('product_id', Integer, ForeignKey(products.id),
 nullable=False))

 stock_table = Table('stocks', metadata,
 Column('id', Integer, primary_key=True),
 Column('name', Unicode(255), nullable=False))


 


--~--~-~--~~~---~--~~
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: how to force a clean refresh of a lazy loaded attribute

2007-02-26 Thread Jonathan Ellis

you could certainly write a helper function if you find that you do this a lot.

I'd be -1 on making it official, it just seems messy to me.

(defining collection only as list is sucky; defining collection
as anything you can iterate over is problematic because an instance
that is itself in the session could define __iter__ and then your
semantics are ambiguous.)

On 2/26/07, Manlio Perillo [EMAIL PROTECTED] wrote:

 This works, thanks!

 However, for non scalar attribute, I would like to simply do:
 sess.expire(myinstance.somecollection)

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