[sqlalchemy] Re: backref error when migrated to 0.4

2007-12-27 Thread Max Ischenko
Hello Bertrand,

It works like a charm, thanks a lot.

Max.

On 12/27/07, Bertrand Croq [EMAIL PROTECTED] wrote:


 JobPosting's mapper tells RefdataLocation's mapper to add a property
 named 'vacancies', then you tell RefdataLocation's mapper to add a
 property
 named 'vacancies'.

 Replace these 2 lines by

 mapper(
 JobPosting,
 jobad_posts_tbl,
 properties = {
 'jb_location': relation(
 RefdataLocation,
 backref = backref(
 'vacancies',
 order_by = desc(jobad_posts_tbl.c.published_date)
 )
 ),
 }
 )

 and it should work as expected.

 --
 Bertrand Croq
 ___
 Net-ng  Tel   : +33 (0)223 21 21 53
 14, rue Patis Tatelin   Fax   : +33 (0)223 21 21 60
 Bâtiment G  Web   : http://www.net-ng.com
 35000 RENNESe-mail: [EMAIL PROTECTED]
 FRANCE


 



-- 
Max
http://maxischenko.in.ua // http://www.linkedin.com/in/maksim

--~--~-~--~~~---~--~~
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: Is there a way to replace object in DB?

2007-12-27 Thread Rick Morrison
I think you're thinking of .load()

   .get() does not throw on not found.



On 12/27/07, braydon fuller [EMAIL PROTECTED] wrote:


 you can also use 'try' to avoid error messages:

 def ensure_object(db, id):
 try:
 o = db.Query(ModelObject).get(id)
 except:
 o = ModelObject(1, u'title')
 db.save(o)
 db.commit()
 return o

 -Braydon


 Rick Morrison wrote:
 
  ...if you're just checking to see if something exists in the database,
  why not just try to .load() it, and then construct it afresh if you
  don't find it?
 
  This kind of operation is sometimes called an upsert ...some
  database engines support it, some don't. Most don't. But what all
  database engines DO support is a query, followed by either an insert,
  or an update as appropriate.
 
  Here's the idiom that should work:
 
  def ensure_object(sess, id):
  o = sess.Query(ModelObject).get(id)# if found, o is now loaded
  into session
  if not o:
  o = ModelObject(1, u'title'
  sess.save(o)
  sess.flush()
  return o
 




 


--~--~-~--~~~---~--~~
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: Is there a way to replace object in DB?

2007-12-27 Thread Denis S. Otkidach

On Dec 26, 2007 10:38 PM, Michael Bayer [EMAIL PROTECTED] wrote:
 if you have an instance which you are unsure if it already exists, you
 can add it to a session using session.save_or_update(instance).  The
 decision between INSERT and UPDATE is ultimately decided by the
 presence of an attribute on the instance called _instance_key.

I'd like mapper to use UPDATE for newly constructed object (i.e.
object without _instance_key attribute) when a record with the same
primary key already exists in DB.

 In most cases, this attribute is not something you need to worry about;
 if an instance has been flushed or loaded from a session, it will have
 the attribute, or if you've just constructed it and not yet persisted
 it, the attribute will not be there.  If you think you need to
 manually manipulate this attribute, perhaps you can describe your
 specific use case so that we can recommend the best way to accomplish
 it.

OK, below is a use/test case:

---8---
import sqlalchemy as sa, logging
from sqlalchemy.orm import mapper, sessionmaker

logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.basicConfig()

class ModelObject(object):

def __init__(self, id, title):
self.id = id
self.title = title

metadata = sa.MetaData()

objectTable = sa.Table(
'Objects', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('title', sa.String(255), nullable=False),
)

objectsMapper = mapper(ModelObject, objectTable)

engine = sa.create_engine('sqlite://')
metadata.create_all(engine, checkfirst=True)

session = sessionmaker(bind=engine)()

obj = ModelObject(1, u'title')
session.save(obj)
session.commit()

session.clear()

# Another program. We have to insure that object with id=1 exists in DB and has
# certain properties.
obj = ModelObject(1, u'title')
session.save_or_update(obj)
session.commit()
---8---

--~--~-~--~~~---~--~~
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: Is there a way to replace object in DB?

2007-12-27 Thread Rick Morrison
...if you're just checking to see if something exists in the database, why
not just try to .load() it, and then construct it afresh if you don't find
it?

This kind of operation is sometimes called an upsert ...some database
engines support it, some don't. Most don't. But what all database engines DO
support is a query, followed by either an insert, or an update as
appropriate.

Here's the idiom that should work:

def ensure_object(sess, id):

o = sess.Query(ModelObject).get(id)# if found, o is now loaded into
session

if not o:

o = ModelObject(1, u'title')

sess.save(o)

sess.flush()

return o



On 12/27/07, Denis S. Otkidach [EMAIL PROTECTED] wrote:


 On Dec 26, 2007 10:38 PM, Michael Bayer [EMAIL PROTECTED] wrote:
  if you have an instance which you are unsure if it already exists, you
  can add it to a session using session.save_or_update(instance).  The
  decision between INSERT and UPDATE is ultimately decided by the
  presence of an attribute on the instance called _instance_key.

 I'd like mapper to use UPDATE for newly constructed object (i.e.
 object without _instance_key attribute) when a record with the same
 primary key already exists in DB.

  In most cases, this attribute is not something you need to worry about;
  if an instance has been flushed or loaded from a session, it will have
  the attribute, or if you've just constructed it and not yet persisted
  it, the attribute will not be there.  If you think you need to
  manually manipulate this attribute, perhaps you can describe your
  specific use case so that we can recommend the best way to accomplish
  it.

 OK, below is a use/test case:

 ---8---
 import sqlalchemy as sa, logging
 from sqlalchemy.orm import mapper, sessionmaker

 logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
 logging.basicConfig()

 class ModelObject(object):

 def __init__(self, id, title):
 self.id = id
 self.title = title

 metadata = sa.MetaData()

 objectTable = sa.Table(
 'Objects', metadata,
 sa.Column('id', sa.Integer, primary_key=True),
 sa.Column('title', sa.String(255), nullable=False),
 )

 objectsMapper = mapper(ModelObject, objectTable)

 engine = sa.create_engine('sqlite://')
 metadata.create_all(engine, checkfirst=True)

 session = sessionmaker(bind=engine)()

 obj = ModelObject(1, u'title')
 session.save(obj)
 session.commit()

 session.clear()

 # Another program. We have to insure that object with id=1 exists in DB
 and has
 # certain properties.
 obj = ModelObject(1, u'title')
 session.save_or_update(obj)
 session.commit()
 ---8---

 


--~--~-~--~~~---~--~~
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: Is there a way to replace object in DB?

2007-12-27 Thread braydon fuller

you can also use 'try' to avoid error messages:

def ensure_object(db, id):
try:
o = db.Query(ModelObject).get(id)
except:
o = ModelObject(1, u'title')
db.save(o)
db.commit()
return o

-Braydon


Rick Morrison wrote:

 ...if you're just checking to see if something exists in the database,
 why not just try to .load() it, and then construct it afresh if you
 don't find it? 

 This kind of operation is sometimes called an upsert ...some
 database engines support it, some don't. Most don't. But what all
 database engines DO support is a query, followed by either an insert,
 or an update as appropriate.

 Here's the idiom that should work:

 def ensure_object(sess, id):
 o = sess.Query(ModelObject).get(id)# if found, o is now loaded
 into session
 if not o:
 o = ModelObject(1, u'title'
 sess.save(o)
 sess.flush()
 return o





--~--~-~--~~~---~--~~
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] filter_by() related table columns

2007-12-27 Thread [EMAIL PROTECTED]

I am attempting to  upgrade from sqlalchemy 0.3.11 to current release
0.4.1 and i am getting the following error:

/recs = session.query(PurchaseOrder).filter_by(description =
'Shipped').all()
 File C:\Python24\Lib\site-packages\sqlalchemy\orm\query.py, line
322, in filter_by
   clauses = [self._joinpoint.get_property(key,
resolve_synonyms=True).compare(operator.eq, value)
 File C:\Python24\Lib\site-packages\sqlalchemy\orm\mapper.py, line
192, in get_property
   raise exceptions.InvalidRequestError(Mapper '%s' has no property
'%s' % (str(self), key))
InvalidRequestError: Mapper 'Mapper|PurchaseOrder|purchaseorder' has
no property 'description'
/
I am trying to query a column in a related table with the following
query:
recs = session.query(PurchaseOrder).filter_by(description =
'Shipped').all()

This query works in 0.3.11.

Is there a new setting when defining relationships that I need to set
so that it looks at the related table columns when running a filter_by
or am i missing something simple?

Here is my simplified example classes, table definitions, and
mappings:

class PurchaseOrder(object) : pass
class OrderStatus(object) : pass

purchaseorder_table = sqlalchemy.Table(
   'purchaseorder',  metadata,
   sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
   sqlalchemy.Column('createdate', sqlalchemy.TIMESTAMP),
   sqlalchemy.Column('statusid', sqlalchemy.Integer,
sqlalchemy.ForeignKey('testing.orderstatus.statusid')),
   schema = 'testing')

orderstatus_table = sqlalchemy.Table(  'orderstatus',
   metadata,
   sqlalchemy.Column('statusid', sqlalchemy.Integer,
primary_key=True),
   sqlalchemy.Column('description', sqlalchemy.VARCHAR),
   schema = 'testing')

orm.mapper(PurchaseOrder,
  purchaseorder_table,
  properties={
   'orderstatus' : orm.relation(OrderStatus)})

orm.mapper(OrderStatus,
  orderstatus_table)

Thanks,
Curtis

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