The following is a stripped down example of my app, that does NOT show the 
problem:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy import create_engine

Base = declarative_base()

class Release(Base):
    __tablename__ = 'releases'
    id = Column(Integer, primary_key=True)
    version = Column(String, nullable=False)
    def __init__(self, version):
        self.version = version
    def __repr__(self):
        return "<Release(version={})>".format(self.version)

class URL(Base):
    __tablename__ = 'urls'
    id = Column(Integer, primary_key=True)
    release_id = Column(Integer, ForeignKey('releases.id'), nullable=False)
    url = Column(String, nullable=False)
    release = relationship("Release",
            cascade="all, delete-orphan",
            single_parent=True,
            backref=backref('urls', order_by=url))
    def __repr__(self):
        return "<URL(url={})>".format(self.url)

def new_release(version, urls):
    r = Release(version)
    l = []
    for url in urls:
        u = URL()
        u.url = url
        l.append(u)
    r.urls = l

    return r

if __name__ == '__main__':
    db = create_engine('sqlite://')
    Base.metadata.create_all(db)
    Session = sessionmaker(db)
    s = Session()
    s.merge(new_release('1.0', ['http://myapp.net/1.0']))
    s.commit()

The real app is basically just a lot more complex (releases have 4 child 
lists like urls, my example adds multiple objects in each list. The app 
fails with an integrity error because a url has a null request_id. In 
actual fact, the hierarchy all gets built up properly in the end, but it 
appears that something is trying to save the children before all of the 
links are set up.

If I remove nullable=False from the foreign keys, it works fine.

I think one of the error tracebacks (sorry, I lost them in other output) 
mentioned autoflush - could the ORM be trying to flush bits of the 
hierarchy before it's complete? Is there a better fix than removing the 
nullable=False constraints?

Thanks for any help,
Paul

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to