06.06.2012 18:06, Michael Bayer kirjoitti:
you need to use the post_update option described at http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#rows-that-point-to-themselves-mutually-dependent-rows .
Thanks for the pointer. Problem solved :)

On Jun 6, 2012, at 1:15 AM, Alex Grönholm wrote:

I have trouble configuring two relationships from one class to another. The following code should be fairly self-explanatory:


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


Base = declarative_base()

class Company(Base):
    __tablename__ = 'companies'
    id = Column(Integer, primary_key=True)
default_address_id = Column(Integer, ForeignKey('addresses.id', use_alter=True, name='defaultaddress_fk')) addresses = relationship('Address', backref='company', primaryjoin='Address.company_id == Company.id') default_address = relationship('Address', primaryjoin='Company.default_address_id == Address.id')


class Address(Base):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey(Company.id), nullable=False)


engine = create_engine('sqlite:///', echo=True)
Base.metadata.create_all(engine)
session = Session(engine)
company = Company()
address = Address()
session.add(company)
company.default_address = address
company.addresses.append(address)
session.flush()


What I expect is SQLAlchemy to 1) create the company, 2) create the address with the new company's id in company_id, 3) assign the ID of the new address to company.default_address_id
Trouble is, I get this error:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected. Cycles: set([ProcessState(ManyToOneDP(Company.default_address), <Company at 0x16a7210>, delete=False), ProcessState(ManyToOneDP(Address.company), <Address at 0x16ad190>, delete=False), SaveUpdateState(<Company at 0x16a7210>), ProcessState(OneToManyDP(Company.addresses), <Company at 0x16a7210>, delete=False), SaveUpdateState(<Address at 0x16ad190>)]) all edges: set([(ProcessState(OneToManyDP(Company.addresses), <Company at 0x16a7210>, delete=False), SaveUpdateState(<Address at 0x16ad190>)), (SaveUpdateState(<Address at 0x16ad190>), ProcessState(ManyToOneDP(Company.default_address), <Company at 0x16a7210>, delete=False)), (SaveUpdateState(<Company at 0x16a7210>), SaveUpdateState(<Address at 0x16ad190>)), (SaveUpdateState(<Company at 0x16a7210>), ProcessState(ManyToOneDP(Address.company), <Address at 0x16ad190>, delete=False)), (ProcessState(ManyToOneDP(Company.default_address), <Company at 0x16a7210>, delete=False), SaveUpdateState(<Company at 0x16a7210>)), (ProcessState(ManyToOneDP(Address.company), <Address at 0x16ad190>, delete=False), SaveUpdateState(<Address at 0x16ad190>)), (SaveUpdateState(<Company at 0x16a7210>), ProcessState(OneToManyDP(Company.addresses), <Company at 0x16a7210>, delete=False))])

What am I doing wrong? I had a similar problem in my production app when trying to delete a Company that had a default address assigned.
I'm on SQLAlchemy 0.7.7.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/fqFKTLBdTYwJ. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

--
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.

--
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.

Reply via email to