Re: [sqlalchemy] CircularDependencyError, why?

2011-09-07 Thread Erkan Özgür Yılmaz
Oh, that worked very well, thank you very much...

E.Ozgur Yilmaz
Lead Technical Director
eoyilmaz.blogspot.com



On Wed, Sep 7, 2011 at 8:04 PM, Michael Bayer wrote:

>
> On Sep 7, 2011, at 12:36 PM, Erkan Özgür Yılmaz wrote:
>
> The following gives CircularDependencyError
>
> where I think there isn't, but you know I don't know that much the
> internals of SQLAlchemy and maybe (and it is a strong possibility) I'm
> wrong. I've ripped of the classes causing the error (copy/paste will
> re-produce the error).
>
>
> According to the error message, TaskableEntity has a foreign key to
> Projects and Projects has a foreign key to TaskableEntity, thus creating a
> cycle.  TaskableEntity would appear to have this Project foreign key via the
> ProjectMixin class.So I'd add a use_alter=True to one of those
> ForeignKey objects.
>
>
>
>
>
>
> from sqlalchemy import orm, create_engine
> from sqlalchemy.orm import relationship, synonym, validates
> from sqlalchemy import (Table, Column, ForeignKey, Boolean, Integer,
> String)
> from sqlalchemy.ext.declarative import (declarative_base, synonym_for,
> declared_attr)
>
>
> Base = declarative_base()
>
>
> 
> class SimpleEntity(Base):
> __tablename__ = "SimpleEntities"
> id = Column("id", Integer, primary_key=True)
>
> entity_type = Column("db_entity_type", String(128), nullable=False)
> __mapper_args__ = {
> "polymorphic_on": entity_type,
> "polymorphic_identity": "SimpleEntity",
> }
>
> 
> class Entity(SimpleEntity):
> __tablename__ = "Entities"
> __mapper_args__ = {"polymorphic_identity": "Entity"}
> entity_id = Column("id", Integer, ForeignKey("SimpleEntities.id"),
>primary_key=True)
>
> 
> class Task(Entity):
> __tablename__ = "Tasks"
> __mapper_args__ = {"polymorphic_identity": "Task"}
> task_id = Column("id", Integer, ForeignKey("Entities.id"),
>  primary_key=True)
> task_of_id = Column(Integer, ForeignKey("TaskableEntities.id"))
>
> 
> class ProjectMixin(object):
> #--
> @declared_attr
> def project_id(cls):
> return Column(
> "project_id",
> Integer,
> ForeignKey("Projects.id"),
> )
>
> #--
> @declared_attr
> def project(cls):
> return relationship(
> "Project",
> primaryjoin=\
> cls.__name__ + ".project_id==Project.project_id_local",
> post_update=True, # for project itself
> uselist=False
> )
>
> #--
> def __init__(self,
>  project=None,
>  **kwargs):
>
> self.project = project
>
> 
> class TaskableEntity(Entity, ProjectMixin):
> __tablename__ = "TaskableEntities"
> __mapper_args__ = {"polymorphic_identity": "TaskableEntity"}
> taskableEntity_id = Column("id", Integer, ForeignKey("Entities.id"),
>primary_key=True)
> tasks = relationship(
> "Task",
> primaryjoin=taskableEntity_id==Task.task_of_id,
> backref="task_of",
> post_update=True,
> )
>
> 
> class Project(TaskableEntity):
> __tablename__ = "Projects"
> project_id_local = Column("id", Integer,
> ForeignKey("TaskableEntities.id"),
>   primary_key=True)
> __mapper_args__ = {"polymorphic_identity": "Project",
>"inherit_condition":
>project_id_local==TaskableEntity.taskableEntity_id}
>
>
> engine = create_engine("sqlite:///:memory:")
> Base.metadata.create_all(engine)
> Session = sqlalchemy.orm.sessionmaker(bind=dengine)
>
> session = Session()
>
>
>
>
> The Error message:
> sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
> cycles:
> set([
> Table('TaskableEntities', MetaData(None),
>   Column('id', Integer(), ForeignKey('Entities.id'),
> table=, primary_key=True, nullable=False),
>   Column('project_id', Integer(), ForeignKey('Projects.id'),
> table=), schema=None),
> Table('Projects', MetaData(None),
>   Column('id', Integer(), ForeignKey('TaskableEntities.id'),
> table=, primary_key=True, nullable=False), schema=None)]
> ) all edges:
> set(
> [
> (Table('TaskableEntities', MetaData(None),
>  

Re: [sqlalchemy] CircularDependencyError, why?

2011-09-07 Thread Michael Bayer

On Sep 7, 2011, at 12:36 PM, Erkan Özgür Yılmaz wrote:

> The following gives CircularDependencyError
> 
> where I think there isn't, but you know I don't know that much the internals 
> of SQLAlchemy and maybe (and it is a strong possibility) I'm wrong. I've 
> ripped of the classes causing the error (copy/paste will re-produce the 
> error).

According to the error message, TaskableEntity has a foreign key to Projects 
and Projects has a foreign key to TaskableEntity, thus creating a cycle.  
TaskableEntity would appear to have this Project foreign key via the 
ProjectMixin class.So I'd add a use_alter=True to one of those ForeignKey 
objects.



 

> 
> from sqlalchemy import orm, create_engine
> from sqlalchemy.orm import relationship, synonym, validates
> from sqlalchemy import (Table, Column, ForeignKey, Boolean, Integer, String)
> from sqlalchemy.ext.declarative import (declarative_base, synonym_for,
> declared_attr)
> 
> 
> Base = declarative_base()
> 
> 
> 
> class SimpleEntity(Base):
> __tablename__ = "SimpleEntities"
> id = Column("id", Integer, primary_key=True)
> 
> entity_type = Column("db_entity_type", String(128), nullable=False)
> __mapper_args__ = {
> "polymorphic_on": entity_type,
> "polymorphic_identity": "SimpleEntity",
> }
> 
> 
> class Entity(SimpleEntity):
> __tablename__ = "Entities"
> __mapper_args__ = {"polymorphic_identity": "Entity"}
> entity_id = Column("id", Integer, ForeignKey("SimpleEntities.id"),
>primary_key=True)
> 
> 
> class Task(Entity):
> __tablename__ = "Tasks"
> __mapper_args__ = {"polymorphic_identity": "Task"}
> task_id = Column("id", Integer, ForeignKey("Entities.id"),
>  primary_key=True)
> task_of_id = Column(Integer, ForeignKey("TaskableEntities.id"))
> 
> 
> class ProjectMixin(object):
> #--
> @declared_attr
> def project_id(cls):
> return Column(
> "project_id",
> Integer,
> ForeignKey("Projects.id"),
> )
> 
> #--
> @declared_attr
> def project(cls):
> return relationship(
> "Project",
> primaryjoin=\
> cls.__name__ + ".project_id==Project.project_id_local",
> post_update=True, # for project itself
> uselist=False
> )
> 
> #--
> def __init__(self,
>  project=None,
>  **kwargs):
> 
> self.project = project
> 
> 
> class TaskableEntity(Entity, ProjectMixin):
> __tablename__ = "TaskableEntities"
> __mapper_args__ = {"polymorphic_identity": "TaskableEntity"}
> taskableEntity_id = Column("id", Integer, ForeignKey("Entities.id"),
>primary_key=True)
> tasks = relationship(
> "Task",
> primaryjoin=taskableEntity_id==Task.task_of_id,
> backref="task_of",
> post_update=True,
> )
> 
> 
> class Project(TaskableEntity):
> __tablename__ = "Projects"
> project_id_local = Column("id", Integer, 
> ForeignKey("TaskableEntities.id"),
>   primary_key=True)
> __mapper_args__ = {"polymorphic_identity": "Project",
>"inherit_condition":
>project_id_local==TaskableEntity.taskableEntity_id}
> 
> 
> engine = create_engine("sqlite:///:memory:")
> Base.metadata.create_all(engine)
> Session = sqlalchemy.orm.sessionmaker(bind=dengine)
> 
> session = Session()
> 
> 
> 
> 
> The Error message:
> sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
> cycles:
> set([
> Table('TaskableEntities', MetaData(None),
>   Column('id', Integer(), ForeignKey('Entities.id'), 
> table=, primary_key=True, nullable=False),
>   Column('project_id', Integer(), ForeignKey('Projects.id'), 
> table=), schema=None),
> Table('Projects', MetaData(None),
>   Column('id', Integer(), ForeignKey('TaskableEntities.id'), 
> table=, primary_key=True, nullable=False), schema=None)]
> ) all edges:
> set(
> [
> (Table('TaskableEntities', MetaData(None),
>Column('id', Integer(), ForeignKey('Entities.id'), 
> table=, primary_key=True, nullable=False),
>Column('project_id'

[sqlalchemy] CircularDependencyError, why?

2011-09-07 Thread Erkan Özgür Yılmaz
The following gives CircularDependencyError

where I think there isn't, but you know I don't know that much the internals
of SQLAlchemy and maybe (and it is a strong possibility) I'm wrong. I've
ripped of the classes causing the error (copy/paste will re-produce the
error).

from sqlalchemy import orm, create_engine
from sqlalchemy.orm import relationship, synonym, validates
from sqlalchemy import (Table, Column, ForeignKey, Boolean, Integer, String)
from sqlalchemy.ext.declarative import (declarative_base, synonym_for,
declared_attr)


Base = declarative_base()



class SimpleEntity(Base):
__tablename__ = "SimpleEntities"
id = Column("id", Integer, primary_key=True)

entity_type = Column("db_entity_type", String(128), nullable=False)
__mapper_args__ = {
"polymorphic_on": entity_type,
"polymorphic_identity": "SimpleEntity",
}


class Entity(SimpleEntity):
__tablename__ = "Entities"
__mapper_args__ = {"polymorphic_identity": "Entity"}
entity_id = Column("id", Integer, ForeignKey("SimpleEntities.id"),
   primary_key=True)


class Task(Entity):
__tablename__ = "Tasks"
__mapper_args__ = {"polymorphic_identity": "Task"}
task_id = Column("id", Integer, ForeignKey("Entities.id"),
 primary_key=True)
task_of_id = Column(Integer, ForeignKey("TaskableEntities.id"))


class ProjectMixin(object):
#--
@declared_attr
def project_id(cls):
return Column(
"project_id",
Integer,
ForeignKey("Projects.id"),
)

#--
@declared_attr
def project(cls):
return relationship(
"Project",
primaryjoin=\
cls.__name__ + ".project_id==Project.project_id_local",
post_update=True, # for project itself
uselist=False
)

#--
def __init__(self,
 project=None,
 **kwargs):

self.project = project


class TaskableEntity(Entity, ProjectMixin):
__tablename__ = "TaskableEntities"
__mapper_args__ = {"polymorphic_identity": "TaskableEntity"}
taskableEntity_id = Column("id", Integer, ForeignKey("Entities.id"),
   primary_key=True)
tasks = relationship(
"Task",
primaryjoin=taskableEntity_id==Task.task_of_id,
backref="task_of",
post_update=True,
)


class Project(TaskableEntity):
__tablename__ = "Projects"
project_id_local = Column("id", Integer,
ForeignKey("TaskableEntities.id"),
  primary_key=True)
__mapper_args__ = {"polymorphic_identity": "Project",
   "inherit_condition":
   project_id_local==TaskableEntity.taskableEntity_id}


engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
Session = sqlalchemy.orm.sessionmaker(bind=dengine)

session = Session()




The Error message:
sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
cycles:
set([
Table('TaskableEntities', MetaData(None),
  Column('id', Integer(), ForeignKey('Entities.id'),
table=, primary_key=True, nullable=False),
  Column('project_id', Integer(), ForeignKey('Projects.id'),
table=), schema=None),
Table('Projects', MetaData(None),
  Column('id', Integer(), ForeignKey('TaskableEntities.id'),
table=, primary_key=True, nullable=False), schema=None)]
) all edges:
set(
[
(Table('TaskableEntities', MetaData(None),
   Column('id', Integer(), ForeignKey('Entities.id'),
table=, primary_key=True, nullable=False),
   Column('project_id', Integer(), ForeignKey('Projects.id'),
table=), schema=None),
 Table('Projects', MetaData(None),
   Column('id', Integer(), ForeignKey('TaskableEntities.id'),
table=, primary_key=True, nullable=False), schema=None)),
(Table('SimpleEntities', MetaData(None),
   Column('id', Integer(), table=,
primary_key=True, nullable=False),
   Column('db_entity_type', String(length=128),
table=, nullable=False), schema=None),
 Table('Entities', MetaData(None),
   Column('id', Integer(), ForeignKey('SimpleEntities.id'),
table=, primary_key=True, nullable=False), schema=None)),
(T