these are all documented functions and I didn't check the exact API.

Here is a full example:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    a = Column(Integer)
    b = Column(Integer)

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

session = Session(e)

session.add(A(a=1, b=2))
session.commit()

a1 = session.query(A).first()

mapper = inspect(a1).mapper

session.expire(a1, [col.key for col in mapper.primary_key])
make_transient(a1)

session.add(a1)
session.commit()



On 03/18/2016 03:54 AM, [email protected] wrote:
Hi Mike,
thanks for idea.
I am getting this exception :
session.expire(test_run, [col.key for col in mapper.primary_keys])
AttributeError: 'InstanceState' object has no attribute 'primary_keys'

Do I need setup something on the schema objects in order to get this
primary_keys?

right now I have this parameters on every relation in the objects in the
schema  :

lazy='joined',cascade="save-update, merge, delete, expunge")



Dňa štvrtok, 17. marca 2016 16:56:05 UTC+1 Mike Bayer napísal(-a):


    how about:


    from sqlalchemy import inspect

    mapper = inspect(obj)

    session.expire(obj, [col.key for col in mapper.primary_keys])
    make_transient(obj)



    On 03/17/2016 10:54 AM, [email protected] <javascript:> wrote:
     >     Right now I am taking  test run object from local database,
    then I
     >     call expunge on that object (cascade is set to expunge).
     >
     >   then I put this object into this python method :
     >
     > def _reset_primary_keys(self, test_run):
     > make_transient(test_run)
     > test_run.ID= None
     >      if test_run.OrderNumber:
     > make_transient(test_run.OrderNumber)
     > test_run.OrderNumber.ID <http://test_run.OrderNumber.ID>= None
     >
     >      for equipmentin test_run.TestEquipments:
     > make_transient(equipment)
     > equipment.ID= None
     >
     >      for trdin test_run.TestRunToDevs:
     > make_transient(trd)
     > trd.ID= None
     >
     >          if trd.TestedDut:
     > make_transient(trd.TestedDut)
     > trd.TestedDut.ID <http://trd.TestedDut.ID>= None
     >
     >          for test_stepin trd.TestSteps:
     > make_transient(test_step)
     > test_step.ID= None
     >
     >              for test_resultin test_step.TestResults:
     > make_transient(test_result)
     > test_result.ID= None
     >
     >
     > This method reset all primary keys, so I can merge it into master
     > database which will generate new primary keys. Is there a better or
     > easier way how to do it?
     >
     > --
     > You received this message because you are subscribed to the Google
     > Groups "sqlalchemy" group.
     > To unsubscribe from this group and stop receiving emails from it,
    send
     > an email to [email protected] <javascript:>
     > <mailto:[email protected] <javascript:>>.
     > To post to this group, send email to [email protected]
    <javascript:>
     > <mailto:[email protected] <javascript:>>.
     > Visit this group at https://groups.google.com/group/sqlalchemy
    <https://groups.google.com/group/sqlalchemy>.
     > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to