please read the ORM tutorial discussion of “relationships” at http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#building-a-relationship <http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#building-a-relationship>. thanks.
> On Dec 9, 2014, at 4:08 AM, alchemy1 <[email protected]> wrote: > > Here is the stack trace, if it can help anyone figure out what I might be > doing wrong: > > Traceback (most recent call last): > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid_debugtoolbar-2.2.2-py3.4.egg/pyramid_debugtoolbar/panels/performance.py", > line 57, in resource_timer_handler > result = handler(request) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid_debugtoolbar-2.2.2-py3.4.egg/pyramid_debugtoolbar/panels/performance.py", > line 87, in profile_handler > result = self.profiler.runcall(handler, request) > File "/usr/lib/python3.4/cProfile.py", line 109, in runcall > return func(*args, **kw) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid/tweens.py", > line 21, in excview_tween > response = handler(request) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py", > line 92, in tm_tween > reraise(*exc_info) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/compat.py", > line 13, in reraise > raise value > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/pyramid_tm-0.8-py3.4.egg/pyramid_tm/__init__.py", > line 80, in tm_tween > manager.commit() > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_manager.py", > line 111, in commit > return self.get().commit() > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py", > line 280, in commit > reraise(t, v, tb) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py", > line 55, in reraise > raise value > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py", > line 271, in commit > self._commitResources() > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py", > line 417, in _commitResources > reraise(t, v, tb) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_compat.py", > line 55, in reraise > raise value > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/transaction-1.4.3-py3.4.egg/transaction/_transaction.py", > line 389, in _commitResources > rm.tpc_begin(self) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/zope.sqlalchemy-0.7.5-py3.4.egg/zope/sqlalchemy/datamanager.py", > line 90, in tpc_begin > self.session.flush() > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py", > line 1919, in flush > self._flush(objects) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py", > line 2037, in _flush > transaction.rollback(_capture_exception=True) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/util/langhelpers.py", > line 60, in __exit__ > compat.reraise(exc_type, exc_value, exc_tb) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/util/compat.py", > line 182, in reraise > raise value > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/session.py", > line 2001, in _flush > flush_context.execute() > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py", > line 372, in execute > rec.execute(self) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py", > line 526, in execute > uow > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/persistence.py", > line 65, in save_obj > mapper, table, insert) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/orm/persistence.py", > line 570, in _emit_insert_statements > execute(statement, multiparams) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/base.py", > line 729, in execute > return meth(self, multiparams, params) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/sql/elements.py", > line 322, in _execute_on_connection > return connection._execute_clauseelement(self, multiparams, params) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/base.py", > line 826, in _execute_clauseelement > compiled_sql, distilled_params > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/base.py", > line 958, in _execute_context > context) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/base.py", > line 1159, in _handle_dbapi_exception > exc_info > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/util/compat.py", > line 188, in raise_from_cause > reraise(type(exception), exception, tb=exc_tb, cause=exc_value) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/util/compat.py", > line 181, in reraise > raise value.with_traceback(tb) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/base.py", > line 951, in _execute_context > context) > File > "/home/user/.virtualenvs/venv/lib/python3.4/site-packages/SQLAlchemy-0.9.8-py3.4-linux-x86_64.egg/sqlalchemy/engine/default.py", > line 436, in do_execute > cursor.execute(statement, parameters) > sqlalchemy.exc.IntegrityError: (IntegrityError) insert or update on table > "child" violates foreign key constraint "child_parent_uuid_fkey" > DETAIL: Key (parent_uuid)=(77e0ed2c-1ec5-45dc-8375-e49677236577) is not > present in table "parent". > 'INSERT INTO child (.............. > > > > > On Saturday, December 6, 2014 9:55:10 PM UTC+1, alchemy1 wrote: > I have an object that has a foreign key to another, declared like > > @declared_attr > def object_uuid(cls): > return Column(UUID, ForeignKey('object.uuid'), nullable=False) > > > I then do something like > > DBSession.add(object) > DBSession.add(dependent_object) > > > > Where DBSession is declared as > > DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) > > > I then get an error > sqlalchemy.exc.IntegrityError: (IntegrityError) insert or update on table > "dependent_object" violates foreign key constraint... > > Am I doing it incorrectly? I think the order of DBSession.add is incorrect, > where I'm adding the main object first then the one with a foreign key to it. > Do I need to somehow commit the first object to the database before the > second one can see it and satisfy the constraint? Ideally it wouldn't be > necessary so that there's not an extra trip to the database. > > (Might be relevant but I hadn't worked on this project in about 6-8 months > and I could have sworn this was working fine then. Today when I pulled down > the dependencies from pip I wonder if some version of something that's newer > might be causing the change.) > > -- > 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 http://groups.google.com/group/sqlalchemy > <http://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]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
