[sqlalchemy] Re: Mapper properties and a callback

2007-12-22 Thread sdobrev

i'm not sure how much this would help u, but 0.4 has better support for 
your-own-collection-containers. see 
http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_collections

e.g. subclass some list and u can do the callback at append() or whatever.

Dave Harrison wrote:
 Hi all,
 
 I have a situation where I want to declare a relation between two
 tables, but I want to be able to run a callback whenever I append to
 that relation.  Here's an example
 
 class Kennel:
 def callback(self, o):
 print callback, o
 
 mapper = Mapper(
 Kennel,
 kennelTable,
 properties = {
 dogs : relation(
 Dog, cascade=all, delete-orphan
 ),
 }
 )
 
 So whenever I use mykennel.dogs.append(fred) I want to be able to tell
 the relation to call callback() so that I can do some checking on the
 object that is being appended.
 
 Is this possible ??
 
 Cheers
 Dave


--~--~-~--~~~---~--~~
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: get mapped class

2007-12-22 Thread sdobrev

Alexandre da Silva wrote:
 
 I am already trying go get the list of mapped tables. I currently got a
 list from sqlalchemy.org.mapper from the weakref mapper_registry, but
 I don't know if that values are useful for my context.
what u need?
all tables? see metadata.
all mappers? see the mapper_registry
the relations inbetween? u have to dig the individual mappers, walk the 
polymorphisms and inheritances.

 I also have another question, I have some way to access the mapped class
 from the table object?
probably no, the relation is directional in the other way,. mapper-table; 
one may have many mappers linked in a way or another to same table.
i'm not sure if u can have 2 separate clasess with 2 primary mappers using 
same table - probably can.


and, why u need all this?


 something like:
 
 user_table = Table(...)
 
 class User(object)
   pass
 
 mapper(User, user_table)
 
 I have some method like user_table.getclass? or another way to get it
 from orm?
 
 thank's for help
 
 


--~--~-~--~~~---~--~~
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: Readonly objects/protecting objects from modifications

2007-12-22 Thread Michael Bayer


On Dec 22, 2007, at 12:34 AM, Andreas Jung wrote:



 --On 21. Dezember 2007 16:33:34 -0500 Michael Bayer [EMAIL PROTECTED] 
  wrote:


 On Dec 21, 2007, at 3:13 PM, Rick Morrison wrote:



 I think the only way something like this should be done is as a test
 fixture which decorates classes during unit tests.It would be
 fairly clumsy to have in production code.

 If you have coworkers who write broken code, the way you solve that  
 is
 by having unit tests which will fail when the coworkers in question  
 do
 something theyre not supposed to.   If other people are writing code
 that sets attrbutes its not supposed to and breaks things, you need
 more tests to catch those conditions.  If youre putting code into
 production that hasnt been tested, then you need a build process,
 automated testing, etc.There is definitely a best practice here
 and test driven development is it.

 With all respect, this is not a useful answer. Even with tests  
 (unittests and weeks of manual tests) I had the case that a simple  
 programming error
 (of my own) produced a data disaster after some weeks. There is no  
 100% test coverage. Tests don't solve all problems. There is  
 sometimes the need for a better security belt.


I am certainly suggesting a fixture that detects illegal assignments  
to attributes.  That it be limited to just unit tests is only a  
suggestion.To establish this functionality regardless of  
environment, like Rick said just create properties which prohibit  
assignment.  Create mappers like this:

class AttrGetter(object):
 def __init__(self, name):
 self.name = name
 def __get__(self, instance, name):
 if instance is None:
 return self
 return getattr(instance, '_' + name)
 def __set__(self, instance, value):
 raise AssertionError(Sets are not allowed)
 def __delete__(self, instance):
 raise AssertionError(Deletes are not allowed)

class MyClass(object):
somecolumn = AttrGetter('somecolumn')
 someothercolumn = AttrGetter('someothercolumn')

mapper(MyClass, sometable, properties={
'_somecolumn':sometable.c.somecolumn,
'_someothercolumn':sometable.c.someothercolumn
})

To automate the above process with no modifications to source code,  
create an instrumented mapper() function which applies the above  
recipe to all table columns:

from sqlalchemy.orm import mapper as _mapper
def mapper(cls, table, **kwargs):
 attrs = {}
 for c in table.c:
 attrs['_' + c.key] = c
 setattr(cls, c.key, AttrGetter(c.key))
 properties = kwargs.setdefault('properties', {})
 properties.update(attrs)
 return _mapper(cls, table, **kwargs)


Hope this helps.

--~--~-~--~~~---~--~~
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] Changing the type/class of an object using polymorphic tables in ORM

2007-12-22 Thread andresj

I don't know if I'm too clear, but I'll try to explain it. I'm using a
set up similar to the one in
http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_mapper_inheritance_joined
. I want to be able to change the type of an object. For example, in
the example in the link above, I want to be able to change an Employee
into an Engineer and viceversa.

Is there a way to do this? Or should I delete the object and create a
new one? And in the last case (create new), how would I be able to
update ForeignKey relationships referencing the changed object?

Thanks in advance for your help.

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

2007-12-22 Thread Anton V. Belyaev

 merge is working rudimentally for objects with unloaded scalar/
 instance/collection attributes in r3974.  whats not yet happening is
 the merging of the various query.options() that may be present on the
 original deferred loader, which means the merged instance wont
 necessarily maintain the exact eager/lazy/deferred loading of the
 original, but this is not especially critical for the basic idea to
 work.

 example script using merge attached.

Michael, thanks a lot for your support!
--~--~-~--~~~---~--~~
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: get mapped class

2007-12-22 Thread Alexandre da Silva

 what u need?
I need the list of mapped Classes, if mapper_registry have all classes I
can use it.
 all tables? see metadata.
 all mappers? see the mapper_registry
 the relations inbetween? u have to dig the individual mappers, walk the 
 polymorphisms and inheritances.

I just want a list containing the relation class-table and
table-class, but I think it's not available, so I will create my own.

 probably no, the relation is directional in the other way,. mapper-table; 
 one may have many mappers linked in a way or another to same table.
 i'm not sure if u can have 2 separate clasess with 2 primary mappers using 
 same table - probably can.

Creating my own mapping list I will get what I need for now.

 and, why u need all this?

At my development scenario I want to create a Form on application just
defining some properties at Mapped Class, and just what I need is a list
with the links table-class, to get one or other depends where am I, 

other thing I need/want is create some database additional objects
(triggers, procedures, etc) just creating a staticmethod at mapped
class, i.e get_addit_metadata()

that's all
for now I will implement my own mapper function, and it will inside add
the class-table to a list to keep tracking and inside call the
sqlalchemy mapper method.


Thank's for all replies


Best Regards,

Alexandre


--~--~-~--~~~---~--~~
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: Changing the type/class of an object using polymorphic tables in ORM

2007-12-22 Thread Michael Bayer


On Dec 22, 2007, at 3:27 PM, andresj wrote:


 I don't know if I'm too clear, but I'll try to explain it. I'm using a
 set up similar to the one in
 http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_mapper_inheritance_joined
 . I want to be able to change the type of an object. For example, in
 the example in the link above, I want to be able to change an Employee
 into an Engineer and viceversa.

 Is there a way to do this? Or should I delete the object and create a
 new one? And in the last case (create new), how would I be able to
 update ForeignKey relationships referencing the changed object?


we dont support this feature right now, its something we will  
implement eventually but its fairly complicated since we have to  
detect the condition, figure out additional tables which require  
INSERTs or DELETEs depending on the class change, etc.

So for now, your best bet is to issue the SQL directly to the database  
for the particular class change you want to do, remove the objects in  
question from the session using expunge(), and reload as the new  
class.   This would involve, for motion from a superclass to subclass  
INSERTing into the desired joined table, for motion from subclass to  
superclass DELETEing from the joined table, or for motion from one  
subclass to another INSERTing and DELETEing.  For deeper inheritance,  
the above pattern becomes more complex.  But I dont think delete and  
create new is necessary here - the base table can remain unchanged  
except for issuing an UPDATE to the discriminator column, assuming  
you have one.

--~--~-~--~~~---~--~~
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: Changing the type/class of an object using polymorphic tables in ORM

2007-12-22 Thread andresj

Thanks, I will use the approach you describe here when I get to that
point in the development proccess :).

 So for now, your best bet is to issue the SQL directly to the database
 for the particular class change you want to do, remove the objects in
 question from the session using expunge(), and reload as the new
 class.
Could you elaborate a bit on using expunge(), please? And when you say
issuing SQL directly to the database you mean using the non-orm part
of sqlalchemy, right?

 This would involve, for motion from a superclass to subclass
 INSERTing into the desired joined table, for motion from subclass to
 superclass DELETEing from the joined table, or for motion from one
 subclass to another INSERTing and DELETEing.
I get what you are saying, just tha tI think that from a superclass to
subclass would involve a DELETE, right? And viceversa, an INSERT...
Unless we are using different terms for superclass and subclass in
which case I apologize :)

 For deeper inheritance,
 the above pattern becomes more complex.  But I dont think delete and
 create new is necessary here - the base table can remain unchanged
 except for issuing an UPDATE to the discriminator column, assuming
 you have one.
I'm happy to know that I don't have to delete and create a new one,
because it would be tedious to update the references...

Thanks for your help

--
Andres

--~--~-~--~~~---~--~~
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: Making it possible to use custom properties in query.filter expressions.

2007-12-22 Thread andresj


On Dec 22, 5:07 pm, Michael Bayer [EMAIL PROTECTED] wrote:
 a synonym() is used so that your attributes are properly available in
 filter().  This functionality has been vastly improved in the current
 SVN trunk which resolves all the issues you outline above, and will be
 available in release 0.4.2.  If you checkout the SVN trunk, you can do:

  class MyAddress(object):
 def _set_email(self, email):
self._email = email
 def _get_email(self):
return self._email
 email = property(_get_email, _set_email)

  mapper(MyAddress, addresses_table, properties = {
  'email':synonym('_email', map_column=True)
  })

 which automatically maps the column to the _email attribute, and
 also instruments the email property with the SQL comparator methods.

Thank's for the info, but is SVN ready for production use (kinda like
Django's SVN, which is recommended by Django)? I mean, can I use it
for my real-world projects without fearing that in any moment it will
not work? And in any case, do you know when is 0.4.2 scheduled to
come?

--
Andres

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