[sqlalchemy] Re: Querying for the existence of an object in the database

2008-04-08 Thread pyplexed

Thanks Michael. The classmethod code worked a treat.

I see merge() has changed in the new release of SA. Does this affect
the solution?
--~--~-~--~~~---~--~~
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: Querying for the existence of an object in the database

2008-04-01 Thread pyplexed

 On Mar 31, 2008, at 12:02 PM, pyplexed wrote:



  The continuing adventures of a newbie SA user...

  Could anyone tell me if there is a simple way of setting up a query
  which takes an object as an argument and checks for the existence of
  an object in the database?

 by primary key ?   assuming as below you don't have any prior
 knowledge of the object's properties or type,
 sess
 .query
 (obj.__class__).get(object_mapper(obj).primary_key_from_instance(obj))

Actually, I would know the type, so maybe that makes it simpler.

I'm in a situation where I want to store data which is generated
externally to my system, and which is polled regularly but individual
items change relatively infrequently. It seemed sensible to try to
quickly spot the items which are already in the database (i.e. all
fields the same), as these require no further action.

I would then process any of the remaining items which have changed
singe the last data update, and create records for the new versions,
and finally insert any completely new items into the database.


  I know I can construct a query hard coding attributes to filter on,
  but I'm sure there is a better way to do this.

 the above is a little ugly so you could make a function that does it,
 given any object (or even make it a classmethod on a base class).

  Ideally, the query would need to handle attributes containing multiple
  objects expressing many to many relationships.

 Im not entirely sure what this means.  You'd like to query for an
 object based on its many-to-many association to another object ?  This
 would look like:

 sess
 .query
 (DesiredClass
 ).filter(DesiredClass.somerelation.contains(some_given_object)).all()

Sorry. I'm still learning the language here. As things stand, my code
just inserts the new records, including some items in tables
associated via many-to-many relationships.

I'd like to add the logic described above, and be able to query the
database using the same externally derived data object as I am
currently saving in the session and database, using it instead
(initially) as a query argument to see if the data it contains already
exists in the database - i.e. it contains no new information to add,
including any data in joined tables..

I wondered if there was a query syntax in SA which would take a full
or partially populated data object and return a match or matches on
the basis of the populated fields. (Null values would probably have to
be ignored, thinking about it).

Being a newbie, I'm not sure if I'm asking something obvious, or if
I'm asking for the Moon on a stick. :-)

I'll take a look at the suggestions you made. Many thanks for them.
--~--~-~--~~~---~--~~
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] Querying for the existence of an object in the database

2008-03-31 Thread pyplexed

The continuing adventures of a newbie SA user...

Could anyone tell me if there is a simple way of setting up a query
which takes an object as an argument and checks for the existence of
an object in the database?

I know I can construct a query hard coding attributes to filter on,
but I'm sure there is a better way to do this.

Ideally, the query would need to handle attributes containing multiple
objects expressing many to many relationships.

I hope the question makes sense.

Many thanks for any 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: Attribute error

2008-03-28 Thread pyplexed

That's fixed it. Thanks very much.

On Mar 26, 7:03 pm, Michael Bayer [EMAIL PROTECTED] wrote:
 On Mar 26, 2008, at 1:34 PM, pyplexed wrote:





  Hi all,

  I'm a complete newcomer to SA, and I've tried to adapt an example I
  found on-line to look a bit more like the problem I'm trying to solve.

  If I save an object to my session, and then flush it, I'm getting an
  error which Google can't help me with:

  AttributeError: 'MetaData' object has no attribute
  'contextual_connect'

  I saw a post that said an attribute error can happen if the Metadata
  class name is mistakenly bound to an instance. I don't think that's
  the case in my script.

  I wondered if anyone here would be able to take a quick look at my (no
  doubt lousy) code and let me know what I'm doing wrong?

  The code is here:

 http://pubcat.org/alchemyTest.py

 sessionmaker needs to bind to the Engine, not the MetaData (this might
 be something we want to detect, its an understandable mistake):

 Session = sessionmaker(bind=engine, autoflush=True, transactional=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
-~--~~~~--~~--~--~---