On 08/24/2016 02:26 PM, lone ois wrote:
M2 = SubTable()

_conn.begin_nested()

M1.mainkey - "00001"

MainTable has no attribute "mainkey" and I think this means to be an = sign.


M2.number = '00001'
M2.name = '00001'

_conn.add(M2)

_conn.commit()
except:
_conn.rollback()
else:
_conn.commit()


(psycopg2.IntegrityError) insert or update on table "SubTable" violates
foreign key constraint "SubTable_MainTable_fkey"
DETAIL: Key (mainkey)=(00001) is not present in table "MainTable".

can't reproduce.  Test case attached, output:

2016-08-24 15:02:44,942 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2016-08-24 15:02:44,943 INFO sqlalchemy.engine.base.Engine SAVEPOINT sa_savepoint_1
2016-08-24 15:02:44,943 INFO sqlalchemy.engine.base.Engine {}
2016-08-24 15:02:44,945 INFO sqlalchemy.engine.base.Engine INSERT INTO "MainTable" (number, name) VALUES (%(number)s, %(name)s) RETURNING "MainTable".id 2016-08-24 15:02:44,945 INFO sqlalchemy.engine.base.Engine {'name': '00001', 'number': '00001'} 2016-08-24 15:02:44,946 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT sa_savepoint_1
2016-08-24 15:02:44,946 INFO sqlalchemy.engine.base.Engine {}
2016-08-24 15:02:44,948 INFO sqlalchemy.engine.base.Engine SAVEPOINT sa_savepoint_2
2016-08-24 15:02:44,948 INFO sqlalchemy.engine.base.Engine {}
2016-08-24 15:02:44,949 INFO sqlalchemy.engine.base.Engine INSERT INTO "SubTable" (mainkey, number, name) VALUES (%(mainkey)s, %(number)s, %(name)s) RETURNING "SubTable".id 2016-08-24 15:02:44,950 INFO sqlalchemy.engine.base.Engine {'mainkey': None, 'number': '00001', 'name': '00001'} 2016-08-24 15:02:44,951 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT sa_savepoint_2
2016-08-24 15:02:44,951 INFO sqlalchemy.engine.base.Engine {}
2016-08-24 15:02:44,951 INFO sqlalchemy.engine.base.Engine COMMIT






the SA echo add(M2) before has BEGIN (implicit) again, and insert data
and rollback savepoint
-------------------------------------------------------------------------
how can save SubTable?
the example is short,
my project subtable has more table data modify.
so SAVEPOINT is must.
but i can't serach any fix
is add(M2) before again BEGIN (implicit) bug ?

I'd need to see a test case that illustrates the actual problem.





--
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.
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class MainTable(Base):
    __tablename__ = 'MainTable'

    id = Column(BIGINT, primary_key = True, autoincrement = True, unique = True)
    number = Column(Text, unique=True, nullable=False)
    name = Column(Text, unique=True, nullable = False)



class SubTable(Base):
    __tablename__ = 'SubTable'

    id = Column(BIGINT, primary_key = True, autoincrement = True, unique = True)
    mainkey = Column(ForeignKey(MainTable.number))
    number = Column(Text, unique = True, nullable = False)
    name = Column(Text, unique = True, nullable = False)

e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

_conn = Session(e)

try:
   M1=MainTable()

   _conn.begin_nested()

   M1.number = '00001'
   M1.name = '00001'
   _conn.add(M1)

   _conn.commit()



   M2 = SubTable()

   _conn.begin_nested()

   M1.mainkey = "00001"
   M2.number = '00001'
   M2.name = '00001'

   _conn.add(M2)

   _conn.commit()
except:
   _conn.rollback()
   raise
else:
   _conn.commit()

Reply via email to