[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

Moshe C. wrote:

 Trying to find out if I hit a bug or it is me doing something wrong.
 Using version 0.4.6

 when creating an object and then calling session.save() I get:
 Instance 'res...@-0x486e4074' is already persistent

 It works save_or_update() with, but I don't see why I should use that.

 I did read that using session.mapper can cause this but I use
 orm.mapper.


session.save() is only used to persist transient instances.   It is
deprecated (as is update() and save_or_update())  and not present in
version 0.5.   Upgrade to at least 0.4.8 if not 0.5 and use session.add()
(equivalent to save_or_update()), which eliminates the need for the user
to distinguish between transient and detached instances.  For a
description of what the heck im talking about when i say transient and
detached see
http://www.sqlalchemy.org/docs/05/session.html#quickie-intro-to-object-states
.




 Here is the mapping code:


 metadata = sa.MetaData()
 sm = orm.sessionmaker(autoflush=True, transactional=True,
 bind=engine)

 Model.session = orm.scoped_session(sm)

 person_table = sa.Table('person', metadata, autoload = True,
 autoload_with=engine)
 person_relative_table = sa.Table('person_relative', metadata,
 autoload = True, autoload_with=engine)
 resume_table = sa.Table('resume', metadata, autoload = True,
 autoload_with=engine)
 workplace_table = sa.Table('workplace', metadata, autoload =
 True, autoload_with=engine)
 resume_workplace_table = sa.Table('resume_workplace',
 metadata, autoload = True, autoload_with=engine)

 orm.mapper(self.Person, person_table, properties = {
 'relatives' : orm.relation(self.Person,
 secondary=person_relative_table,

 primaryjoin=person_table.c.id==person_relative_table.c.person_id,

 secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
  backref='followers'),
 'resumes' : orm.relation(self.Resume, backref='person')
 }
)
 orm.mapper(self.Resume, resume_table, properties = {
 'workplaces' : orm.relation(self.Workplace,
 secondary=resume_workplace_table, backref='resumes')
 }
)
 orm.mapper(self.Workplace, workplace_table)



 



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Moshe C.

Thanks.
Given that I am not going to upgrade very soon, is it right to
conclude that there was a bug in 0.4.6, or is my usage wrong?


On Jun 3, 5:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Trying to find out if I hit a bug or it is me doing something wrong.
  Using version 0.4.6

  when creating an object and then calling session.save() I get:
  Instance 'res...@-0x486e4074' is already persistent

  It works save_or_update() with, but I don't see why I should use that.

  I did read that using session.mapper can cause this but I use
  orm.mapper.

 session.save() is only used to persist transient instances.   It is
 deprecated (as is update() and save_or_update())  and not present in
 version 0.5.   Upgrade to at least 0.4.8 if not 0.5 and use session.add()
 (equivalent to save_or_update()), which eliminates the need for the user
 to distinguish between transient and detached instances.  For a
 description of what the heck im talking about when i say transient and
 detached 
 seehttp://www.sqlalchemy.org/docs/05/session.html#quickie-intro-to-objec...
 .



  Here is the mapping code:

          metadata = sa.MetaData()
          sm = orm.sessionmaker(autoflush=True, transactional=True,
  bind=engine)

          Model.session = orm.scoped_session(sm)

          person_table = sa.Table('person', metadata, autoload = True,
  autoload_with=engine)
          person_relative_table = sa.Table('person_relative', metadata,
  autoload = True, autoload_with=engine)
          resume_table = sa.Table('resume', metadata, autoload = True,
  autoload_with=engine)
          workplace_table = sa.Table('workplace', metadata, autoload =
  True, autoload_with=engine)
          resume_workplace_table = sa.Table('resume_workplace',
  metadata, autoload = True, autoload_with=engine)

          orm.mapper(self.Person, person_table, properties = {
              'relatives' : orm.relation(self.Person,
  secondary=person_relative_table,

  primaryjoin=person_table.c.id==person_relative_table.c.person_id,

  secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
                                       backref='followers'),
              'resumes' : orm.relation(self.Resume, backref='person')
              }
                     )
          orm.mapper(self.Resume, resume_table, properties = {
              'workplaces' : orm.relation(self.Workplace,
  secondary=resume_workplace_table, backref='resumes')
              }
                     )
          orm.mapper(self.Workplace, workplace_table)
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

Moshe C. wrote:

 Thanks.
 Given that I am not going to upgrade very soon, is it right to
 conclude that there was a bug in 0.4.6, or is my usage wrong?

it is not a bug.   save() is used only for transient instances.




 On Jun 3, 5:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Trying to find out if I hit a bug or it is me doing something wrong.
  Using version 0.4.6

  when creating an object and then calling session.save() I get:
  Instance 'res...@-0x486e4074' is already persistent

  It works save_or_update() with, but I don't see why I should use that.

  I did read that using session.mapper can cause this but I use
  orm.mapper.

 session.save() is only used to persist transient instances.   It is
 deprecated (as is update() and save_or_update())  and not present in
 version 0.5.   Upgrade to at least 0.4.8 if not 0.5 and use
 session.add()
 (equivalent to save_or_update()), which eliminates the need for the user
 to distinguish between transient and detached instances.  For a
 description of what the heck im talking about when i say transient and
 detached
 seehttp://www.sqlalchemy.org/docs/05/session.html#quickie-intro-to-objec...
 .



  Here is the mapping code:

          metadata = sa.MetaData()
          sm = orm.sessionmaker(autoflush=True, transactional=True,
  bind=engine)

          Model.session = orm.scoped_session(sm)

          person_table = sa.Table('person', metadata, autoload = True,
  autoload_with=engine)
          person_relative_table = sa.Table('person_relative', metadata,
  autoload = True, autoload_with=engine)
          resume_table = sa.Table('resume', metadata, autoload = True,
  autoload_with=engine)
          workplace_table = sa.Table('workplace', metadata, autoload =
  True, autoload_with=engine)
          resume_workplace_table = sa.Table('resume_workplace',
  metadata, autoload = True, autoload_with=engine)

          orm.mapper(self.Person, person_table, properties = {
              'relatives' : orm.relation(self.Person,
  secondary=person_relative_table,

  primaryjoin=person_table.c.id==person_relative_table.c.person_id,

  secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
                                       backref='followers'),
              'resumes' : orm.relation(self.Resume, backref='person')
              }
                     )
          orm.mapper(self.Resume, resume_table, properties = {
              'workplaces' : orm.relation(self.Workplace,
  secondary=resume_workplace_table, backref='resumes')
              }
                     )
          orm.mapper(self.Workplace, workplace_table)
 



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Moshe C.

Well, I thought it was transient.

If you refer to the code in the first post:
If I create a new Resume object and save() it , it works.
If before the save(), I fetch a Workplace object from the DB, then save
() fails and I need to use save_and_update().

So the fact that I queried the DB for a related object, makes the
Resume object not transient anymore?


On Jun 3, 8:56 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Thanks.
  Given that I am not going to upgrade very soon, is it right to
  conclude that there was a bug in 0.4.6, or is my usage wrong?

 it is not a bug.   save() is used only for transient instances.



  On Jun 3, 5:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  Moshe C. wrote:

   Trying to find out if I hit a bug or it is me doing something wrong.
   Using version 0.4.6

   when creating an object and then calling session.save() I get:
   Instance 'res...@-0x486e4074' is already persistent

   It works save_or_update() with, but I don't see why I should use that.

   I did read that using session.mapper can cause this but I use
   orm.mapper.

  session.save() is only used to persist transient instances.   It is
  deprecated (as is update() and save_or_update())  and not present in
  version 0.5.   Upgrade to at least 0.4.8 if not 0.5 and use
  session.add()
  (equivalent to save_or_update()), which eliminates the need for the user
  to distinguish between transient and detached instances.  For a
  description of what the heck im talking about when i say transient and
  detached
  seehttp://www.sqlalchemy.org/docs/05/session.html#quickie-intro-to-objec...
  .

   Here is the mapping code:

           metadata = sa.MetaData()
           sm = orm.sessionmaker(autoflush=True, transactional=True,
   bind=engine)

           Model.session = orm.scoped_session(sm)

           person_table = sa.Table('person', metadata, autoload = True,
   autoload_with=engine)
           person_relative_table = sa.Table('person_relative', metadata,
   autoload = True, autoload_with=engine)
           resume_table = sa.Table('resume', metadata, autoload = True,
   autoload_with=engine)
           workplace_table = sa.Table('workplace', metadata, autoload =
   True, autoload_with=engine)
           resume_workplace_table = sa.Table('resume_workplace',
   metadata, autoload = True, autoload_with=engine)

           orm.mapper(self.Person, person_table, properties = {
               'relatives' : orm.relation(self.Person,
   secondary=person_relative_table,

   primaryjoin=person_table.c.id==person_relative_table.c.person_id,

   secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
                                        backref='followers'),
               'resumes' : orm.relation(self.Resume, backref='person')
               }
                      )
           orm.mapper(self.Resume, resume_table, properties = {
               'workplaces' : orm.relation(self.Workplace,
   secondary=resume_workplace_table, backref='resumes')
               }
                      )
           orm.mapper(self.Workplace, workplace_table)
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

Moshe C. wrote:

 Well, I thought it was transient.

 If you refer to the code in the first post:

your first post has a mapping only.  There is no illustration of how
you're querying, or using save() or load.



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Moshe C.

In code:

t = Model.Resume()
t.id = something

w = Model.session.query(Model.Workplace).filter_by(id=idd).first()

# model.save(t)
model.save_or_update(t)


Without the query line, save() would have worked, but the query is on
another object. There is a relation between the objects, but it is not
clear how querying on another object makes the Resume object non-
transient.




On Jun 3, 10:55 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Well, I thought it was transient.

  If you refer to the code in the first post:

 your first post has a mapping only.  There is no illustration of how
 you're querying, or using save() or load.
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

Moshe C. wrote:

 In code:

 t = Model.Resume()
 t.id = something

   w = Model.session.query(Model.Workplace).filter_by(id=idd).first()

 # model.save(t)
 model.save_or_update(t)


 Without the query line, save() would have worked, but the query is on
 another object. There is a relation between the objects, but it is not
 clear how querying on another object makes the Resume object non-
 transient.

if you're using ScopedSession.mapper, which I really, really, really think
you should not, then when you say Model.Resume(), it gets added to the
session immediately - the query() would then autoflush it.








 On Jun 3, 10:55 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Well, I thought it was transient.

  If you refer to the code in the first post:

 your first post has a mapping only.  There is no illustration of how
 you're querying, or using save() or load.
 



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Moshe C.

Weird, the first assertion already fails, but I am not using
ScopedSession.mapper. See the code in the first post.

On Jun 3, 11:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 easy way to ensure things are working as expected:

 Moshe C. wrote:

  In code:

          t = Model.Resume()
          t.id = something

           assert t not in Model.session



     w = Model.session.query(Model.Workplace).filter_by(id=idd).first()

           assert t not in Model.session

          # model.save(t)
          model.save_or_update(t)
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

Moshe C. wrote:

 Weird, the first assertion already fails, but I am not using
 ScopedSession.mapper. See the code in the first post.

 On Jun 3, 11:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 easy way to ensure things are working as expected:

 Moshe C. wrote:

  In code:

          t = Model.Resume()
          t.id = something

           assert t not in Model.session

its not a full example.  no imports are illustrated including what
orm.mapper() might be doing.   There is obviously code which is setting up
Session.mapper() or otherwise code within Resume().__init__() doing
something similar.











     w = Model.session.query(Model.Workplace).filter_by(id=idd).first()

           assert t not in Model.session

          # model.save(t)
          model.save_or_update(t)
 



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Moshe C.

full code:

import sqlalchemy as sa
from sqlalchemy import orm

class Model:
session = None

class Person(object):
@staticmethod
def query():
return Model.session.query(Model.Person)

class PersonRelative(object):
@staticmethod
def query():
return Model.session.query(Model.PersonRelative)

class Resume(object):
@staticmethod
def query():
return Model.session.query(Model.Resume)

class Workplace(object):
@staticmethod
def query():
return Model.session.query(Model.Workplace)

class ResumeWorkplace(object):
@staticmethod
def query():
return Model.session.query(Model.ResumeWorkplace)


def __init__(self, engine):
Call me before using any of the tables or classes in the
model.

metadata = sa.MetaData()
sm = orm.sessionmaker(autoflush=True, transactional=True,
bind=engine)

Model.session = orm.scoped_session(sm)

person_table = sa.Table('person', metadata, autoload = True,
autoload_with=engine)
person_relative_table = sa.Table('person_relative', metadata,
autoload = True, autoload_with=engine)
resume_table = sa.Table('resume', metadata, autoload = True,
autoload_with=engine)
workplace_table = sa.Table('workplace', metadata, autoload =
True, autoload_with=engine)
resume_workplace_table = sa.Table('resume_workplace',
metadata, autoload = True, autoload_with=engine)

orm.mapper(self.Person, person_table, properties = {
'relatives' : orm.relation(self.Person,
secondary=person_relative_table,
 
primaryjoin=person_table.c.id==person_relative_table.c.person_id,
 
secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
 backref='followers'),
'resumes' : orm.relation(self.Resume, backref='person')
}
   )
orm.mapper(self.Resume, resume_table, properties = {
'workplaces' : orm.relation(self.Workplace,
secondary=resume_workplace_table, backref='resumes')
}
   )
orm.mapper(self.Workplace, workplace_table)



def commit(self):
Model.session.commit()

def save(self, obj):
Model.session.save(obj)

def save_or_update(self, obj):
Model.session.save_or_update(obj)

def flush(self):
Model.session.flush()

def delete(self, obj):
Model.session.delete(obj)

def clear(self):
Model.session.clear()


On Jun 3, 11:47 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Weird, the first assertion already fails, but I am not using
  ScopedSession.mapper. See the code in the first post.

  On Jun 3, 11:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  easy way to ensure things are working as expected:

  Moshe C. wrote:

   In code:

           t = Model.Resume()
           t.id = something

            assert t not in Model.session

 its not a full example.  no imports are illustrated including what
 orm.mapper() might be doing.   There is obviously code which is setting up
 Session.mapper() or otherwise code within Resume().__init__() doing
 something similar.



      w = Model.session.query(Model.Workplace).filter_by(id=idd).first()

            assert t not in Model.session

           # model.save(t)
           model.save_or_update(t)
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: needed save_or_update(), save() didn't work

2009-06-03 Thread Michael Bayer

that code is fine as far as I can see.   if you are still having problems,
produce a test script that can be run by others (this does not qualify
since it uses autoload and does not illustrate the session usage nor the
creation of Model).


Moshe C. wrote:

 full code:

 import sqlalchemy as sa
 from sqlalchemy import orm

 class Model:
 session = None

 class Person(object):
 @staticmethod
 def query():
 return Model.session.query(Model.Person)

 class PersonRelative(object):
 @staticmethod
 def query():
 return Model.session.query(Model.PersonRelative)

 class Resume(object):
 @staticmethod
 def query():
 return Model.session.query(Model.Resume)

 class Workplace(object):
 @staticmethod
 def query():
 return Model.session.query(Model.Workplace)

 class ResumeWorkplace(object):
 @staticmethod
 def query():
 return Model.session.query(Model.ResumeWorkplace)


 def __init__(self, engine):
 Call me before using any of the tables or classes in the
 model.

 metadata = sa.MetaData()
 sm = orm.sessionmaker(autoflush=True, transactional=True,
 bind=engine)

 Model.session = orm.scoped_session(sm)

 person_table = sa.Table('person', metadata, autoload = True,
 autoload_with=engine)
 person_relative_table = sa.Table('person_relative', metadata,
 autoload = True, autoload_with=engine)
 resume_table = sa.Table('resume', metadata, autoload = True,
 autoload_with=engine)
 workplace_table = sa.Table('workplace', metadata, autoload =
 True, autoload_with=engine)
 resume_workplace_table = sa.Table('resume_workplace',
 metadata, autoload = True, autoload_with=engine)

 orm.mapper(self.Person, person_table, properties = {
 'relatives' : orm.relation(self.Person,
 secondary=person_relative_table,

 primaryjoin=person_table.c.id==person_relative_table.c.person_id,

 secondaryjoin=person_relative_table.c.relative_id==person_table.c.id,
  backref='followers'),
 'resumes' : orm.relation(self.Resume, backref='person')
 }
)
 orm.mapper(self.Resume, resume_table, properties = {
 'workplaces' : orm.relation(self.Workplace,
 secondary=resume_workplace_table, backref='resumes')
 }
)
 orm.mapper(self.Workplace, workplace_table)



 def commit(self):
 Model.session.commit()

 def save(self, obj):
 Model.session.save(obj)

 def save_or_update(self, obj):
 Model.session.save_or_update(obj)

 def flush(self):
 Model.session.flush()

 def delete(self, obj):
 Model.session.delete(obj)

 def clear(self):
 Model.session.clear()


 On Jun 3, 11:47 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Moshe C. wrote:

  Weird, the first assertion already fails, but I am not using
  ScopedSession.mapper. See the code in the first post.

  On Jun 3, 11:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  easy way to ensure things are working as expected:

  Moshe C. wrote:

   In code:

           t = Model.Resume()
           t.id = something

            assert t not in Model.session

 its not a full example.  no imports are illustrated including what
 orm.mapper() might be doing.   There is obviously code which is setting
 up
 Session.mapper() or otherwise code within Resume().__init__() doing
 something similar.



      w =
 Model.session.query(Model.Workplace).filter_by(id=idd).first()

            assert t not in Model.session

           # model.save(t)
           model.save_or_update(t)
 



--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---