[sqlalchemy] Re: problems with mapper inheritance and eager loader

2007-01-29 Thread Michael Bayer


On Jan 29, 2007, at 11:30 AM, Manlio Perillo wrote:



 The problem is that sometimes (at random, but I'm not sure),  
 SQLAlchemy
 wants to use the lazy loader for comments, when I do session.load 
 (Derived).


thats sometimes due to an eager degrade.  eager loading will  
generally not issue its LEFT OUTER JOIN if constructing the query  
means it will loop back to the originating table in one query...it  
will stop at the point before it gets there.   this kind of thing  
actually happens a lot.  this can occur when using backrefs...if you  
have A and B with eager refs to each other, and you load an A with  
its B's, hitting the A collection on each B will incur as a lazy  
load...since the eager loader isnt going to eager load A-B-A-B-A...

but im not sure if thats whats happening here.  maybe something up  
with load(), id guess if the instance is already in the session  
maybe.  it would help if you could show me more fully what youre doing.






--~--~-~--~~~---~--~~
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 and business object verification??

2007-01-29 Thread chris e



On Jan 29, 1:49 am, Julien Cigar [EMAIL PROTECTED] wrote:
 Michael Bayer wrote:
  On Jan 24, 1:50 am, chris e [EMAIL PROTECTED] wrote:

  I am planning on using sqlalchemy to build the api for a database I am
  developing, and I was wondering if there is any type of column
  verification that occurs before database commit.

  I.E.:  a string column with length 40 would throw a verification
  exception if a value longer that 40 characters was placed into it and
  saved.

  your database will throw an error.  why reinvent the wheel ?

  Additionally has anyone thought of implementing some sort of
  verification support for objects that are mapped which would allow the
  object to perform pre database action logic?  The intent being that the
  instance would check to see that the object meets additional business
  logic requirements before it is inserted or updated.

  I.E.: a User business object would verify that the userid was part of
  the [a-z] [A-Z]and [0-9] character classes, and if not an exception
  would be raised to prevent the database action.

  thats exactly the kind of thing you should write into your
  application.  has nothing to do with an ORM.  for generic validation
  widgets to help, check out formencode (http://formencode.org/).(Just to 
  share a method which woks well for me :)

 What I usually to perform validation is to create a property()
 (_set_attribute(), _get_attribute()) for each mapped column, then I use
 the column_prefix=_ attribute in SQLAlchemy.
 After that I have a function which iterate on the columns
 (YourMappedObject.c.keys()) and use a try / except with a setattr (it's
 a bit more sophisticated than that in fact), for example :

 assign_mapper(session_context, Language, table_languages, column_prefix='_')

 class Language(object):

   def _set_iso_code(self, value):
 try:
   value = ''.join(value.split()).lower()
 except AttributeError:
   raise Invalid('iso code must be a string')
 if len(value) == 2:
   self._iso_code = value
 else:
   raise Invalid('Invalid iso code')

   def _get_iso_code(self):
 return self._iso_code

   iso_code = property(_get_iso_code, _set_iso_code)

 (...)

 then I do something like (not complete):
 def populate(MappedObject, values):
   errors = []
   for c in MappedObject.c.keys():
 value = values.get(c, Undefined())
 if value is not Undefined:
   try:
 setattr(MappedObject, c , value)
   except Invalid, e:
 errors.append(str(e))
   return errors

 also, I have in my models a __before_save__ / __before_update__ which
 check additional things like NOT NULL constraints (!None), ...

 --
 Julien Cigar
 Belgian Biodiversity Platformhttp://www.biodiversity.be
 Université Libre de Bruxelles
 Campus de la Plaine CP 257
 Bâtiment NO, Bureau 4 N4 115C (Niveau 4)
 Boulevard du Triomphe, entrée ULB 2
 B-1050 Bruxelles
 office: [EMAIL PROTECTED]
 home: [EMAIL PROTECTED]

Thanks for the info. After I did a bit more reading I realised that I 
would have to build a custom mapper, I will definitely take your 
suggestions into account when I write it.


--~--~-~--~~~---~--~~
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: polymorphic mapping with more than 2 level of inheritance

2007-01-29 Thread svilen
here's something about postgres:

pu_A = polymorphic_union( {
'A': table_A,
'B': table_B,
}, 'atype', 'pu_A', ) #concrete

On postgres, the above fails - does not like the 'pu_A'. 
If it's lowercase/omitted, it's ok.

i think u did already say that about mixed casing in some post above;
should i just scratch that name/alias (it is there only for clarity)?


$ python _test_ABC_all.py db=postgres:///proba 

SQLError: (ProgrammingError) missing FROM-clause entry for 
table pu_a
 'SELECT pu_A.id AS pu_A_id, pu_A.linkA_id AS pu_A_linkA_id, 
pu_A.atype AS pu_A_atype, pu_A.name AS pu_A_name, pu_A.data2 AS 
pu_A_data2 \nFROM (SELECT CAST(NULL AS TEXT) AS data2, A.linkA_id 
AS linkA_id, A.name AS name, A.id AS id, \'A\' AS atype 
\nFROM A UNION ALL SELECT B.data2 AS data2, B.linkA_id 
AS linkA_id, B.name AS name, B.id AS id, \'B\' AS atype 
\nFROM B) AS pu_A \nWHERE pu_A.id = %(pu_A_id)s ORDER 
BY pu_A.id \n LIMIT 1' {'pu_A_id': 1L}


 all tests pass with rev 2267 of that branch.  try that rev
 specifically, since i want to take whats there and do another pass.
 im trying to get it so that the entire science of parent table,
 child table, polymorphic selectables, primary join - polymorphic
 joins - determine direction/lazy clause/eager clause/synchronize
 FKs is super-well-nailed down.


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



_test-pg-punion-mixedcasename.py
Description: application/python


sa_gentestbase.py
Description: application/python


[sqlalchemy] Re: polymorphic mapping with more than 2 level of inheritance

2007-01-29 Thread sdobrev
here next portion - 3 kinds, one case per kind.

 all tests pass with rev 2267 of that branch.  try that rev
 specifically, since i want to take whats there and do another pass.
 im trying to get it so that the entire science of parent table,
 child table, polymorphic selectables, primary join - polymorphic
 joins - determine direction/lazy clause/eager clause/synchronize
 FKs is super-well-nailed down.


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



sa_gentestbase.py
Description: application/python


tABC-3.py
Description: application/python


[sqlalchemy] Re: polymorphic mapping with more than 2 level of inheritance

2007-01-29 Thread sdobrev


 here next portion - 3 kinds, one case per kind.
hm.hmmm. on sqlite they were 3 failing - now only 2;
on postgres all 3 fail...

The cirlcular dep one - is there because i cannot get where the 
circ-dep is. The post-updates are invented by the mincut algo, and i 
don't see why such configuration is not working.

Maybe something related to the not-really-inherited 
but-inheriting-something relations for concrete mappers? A.linkA is 
not post_update, while the B.linkA and C.linkA are.
It goes away if A.linkA also gets a post_update.

These are from all-possibilities for C.linkC=None, no_eager

--~--~-~--~~~---~--~~
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] Informix SA backend

2007-01-29 Thread askel

Hello everybody,

I have done some initial implementation of Informix as one of SA 
backend. It is currently using InformixDB 2.3. I'd like to share what 
was already implemented with SA community. Database schema reflection 
needs pretty much work. Also, I don't have access to anything but IDS 
9.40 and it might not work well for other Informix versions/flavours. 
This is time for other interested people to get involved in that.

Alexander


--~--~-~--~~~---~--~~
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: #446: Two deletions cascading to the same object can result in an error

2007-01-29 Thread Michael Bayer



On Jan 29, 8:19 pm, Ian Kelly [EMAIL PROTECTED] wrote:

 I can live with this response, but I still consider it a bug.  If I
 have to manually remove the instances whenever I delete an object
 where this exception is possible, then I might as well just be doing
 the entire cascade for the affected relation manually -- it's
 essentially the same logic.  In fact, that's what I ended up resorting
 to in my workaround for this (although please let me know if there's a
 better way).

I have a small paragraph about this in the session docs (notes on 
flush) , the basic idea is that SA in almost all cases will never 
manipulate any portion of the composition of your objects in memory.  
the one exception to this is backreferences, which is a feature that 
takes place almost entirely separate from all the unit-of-work flush 
logic.  for SA to start handling more than that would be out of the 
scope of SA's main job of mapping objects to tables and issuing 
SQL..it would become its own object-oriented frameworkthe level of 
magical behavior observed would be overwhelming to produce, 
maintain, and document.

SA also takes a lot of cues from Hibernate which simlarly would never 
have anything to do with manipulating collections 
automatically...Hibernate also makes you write your own bi-directional 
relationships manually.

its unusual in the first place that you have an object which is the 
target of more than one cascading delete operation...most database 
designs usually have singular parent/child relationships, or if a 
single parent has many kinds of child types associated with it, then 
delete operations usually begin with that parent and then cascade to 
the children.

 Finally, note that I'm _not_ asking for sqlalchemy to maintain the
 collections for me.  All I'm asking is for the cascade code not to
 attempt to delete objects that have already been deleted and flushed,
 or at least to safely handle the exception it raises when it does.

OK, what behavior are you looking for  ?  it raises an exception right 
now.  whats unsafe about it ?

At
 least in my particular application, I'm willing to live with the
 collections being inaccurate.

silent failure is definitely not an option here...SA aims to be as 
explicit as possible, not just because its a central Python tenet but 
its just good programming practice.  Hibernate is a lot less friendly 
in this area too.

The problem is that the cascade code is
 not so willing.  The thing that strikes me as odd about this state of
 affairs is that sqlalchemy potentially ends up forcing upon me the
 same overhead that the policy is intended to avoid.

the cascade behavior is very closely modeled to Hibernate, which I 
take as close to a best practice as I am currently aware.  not that 
SA cant go beyond what it does, but each step beyond must be 
considered alot more carefully since its uncharted territorypoorly 
considered features and only partially-complete functionalities become 
disastrous, which is why i usually try to not go there without a 
full plan to implement something in its entirety.  and this one, every 
time it comes up, just sounds like it would need to become an entire 
in-memory object management system, not something im up for creating 
right now (and would not be core SA anyway).

so in this case, I dont see any core behavior to be built here since 
my gut says it would be fundamentally incomplete and only create more 
confusion, but perhaps some Wiki recipies or extensions can help users 
out.  if you want to post some of your common use cases maybe some of 
those patterns can be fleshed out.



--~--~-~--~~~---~--~~
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: Informix SA backend

2007-01-29 Thread Michael Bayer

if you click on the login link near the upper right, and log in with 
the username/password guest/guest, the view of the ticket will 
produce a comments box as well as a button in which you can upload 
attachments.  you can attach the file(s) for your implementation, for 
example.

On Jan 29, 9:35 pm, askel [EMAIL PROTECTED] wrote:
 On Jan 29, 9:01 pm, Michael Bayer [EMAIL PROTECTED] wrote:

  feel free to update the ticket for this:

 http://www.sqlalchemy.org/trac/ticket/336I'm terribly sorry but I have no 
 idea how do I do that. I submitted
 that ticket some time ago but I can't see any option when I follow
 that link.


--~--~-~--~~~---~--~~
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] work directly with table clauses in transactions?

2007-01-29 Thread Kumar McMillan

I'm trying to work on something where it's necessary to discover table
objects and then perform inserts and deletes in a transaction.  It's
proving very cumbersome to try and locate the existing mappers for
these tables or create them automatically so I'm now trying to work
directly with table.insert(), table.delete(), etc.  Specifically, I
was getting stuck when a mapper for a table had already been used to
create an object earlier on (seems harmless to insert more objects
later, but I don't have access to the original mapper at that point).

I can't seem to figure out how to do something like
table.insert().execute() inside a transaction.  I already have code
that starts with self.session.create_transaction() and then does all
the inserts/deletes effectively with mappers but this doesn't seem to
work the same for table clauses.

Can someone point me at the right code to read or a page in the docs
for executing clauses in transactions?  I've been looking mostly at
http://www.sqlalchemy.org/docs/sqlconstruction.myt#sql_select ...  my
best guess is that I need to run table.insert().compile() somehow with
a transaction created by an engine object?

thanks, Kumar

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