[sqlalchemy] One to One relation problem

2020-12-17 Thread 'Sören Textor' via sqlalchemy
Hello I have a huge problem with süecific "one to one" relation. Woking (it's the tutorial code) class Son(db.Model): __tablename__ = 'son' id = db.Column(db.Integer, primary_key=True) papa_id = db.Column(db.Integer, db.ForeignKey('papa.id')) papa = db.relationship("Papa", foreig

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread Mike Bayer
perhaps you are looking to set viewonly=True on this second relationship? that will exclude it from any kind of persistence operation.it will only be used to load things in from the database but not accept or honor any mutations of data. not sure why you need to have two relationships that

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread maqui...@gmail.com
My operating assumption is that sqlalchemy looks at each relationship and tries to delete it, but since the previous relationship to the same base class was already deleted, it throws the exception and the session rolls back. The error from above is essentially the same as the actual error in m

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread maqui...@gmail.com
1. target database = postgres, example queries and stacktrace: >>> from webapp.database.orm.models import ParentClass, ChildClass, ChildChildClass >>> p = ParentClass() >>> c = ChildClass() >>> cc = ChildChildClass() >>> c.children.append(cc) >>> p.children.append(c) >>> session.add(p) >>> sess

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread Mike Bayer
On Thu, Dec 17, 2020, at 7:15 PM, Mark Aquino wrote: > They seem to be runnable except the base should be= DeclarativeMeta OK, runnable example is below. how about: 1. target database type (Postgresql?) 2. sample data, inserts, etc. 3. sample queries 4. operation that fails 5. stack trac

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread Mark Aquino
They seem to be runnable except the base should be DeclarativeMeta The relationships aren’t to the same target in real life, It’s like this: Class BaseClass: ... Class SubClassA(BaseClass) ... Class SubclassB(BaseClass): ... (Plus Many other subclasses) A Mixer “BaseClassBaseClass” such that

Re: [sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread Mike Bayer
your examples aren't complete or runnable so I don't really know what the issue is, although having two relationships to the same target class seems a little unusual and I'm not sure why you'd need that. On Thu, Dec 17, 2020, at 6:01 PM, maqui...@gmail.com wrote: > > I think I may have just fo

[sqlalchemy] Re: Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread maqui...@gmail.com
I think I may have just found a solution? Not sure if this is correct but it looks like it worked when i changed the "extra" relationship to passive_deletes=True instead of cascade class ChildClass(XPressoBase): __tablename__ = "child_class" id = Column("id", UUID(as_uuid=True), primary_key=Tru

[sqlalchemy] Can't delete cascade many-to-many with polymorphic relationships unless using lazy=dynamic

2020-12-17 Thread maqui...@gmail.com
I have a polymorphic data model where association tables are to base classes and some relationships that link to different child classes to filter out the non-matching base classes, however these seem to result in sqlalchemy being unable to delete cascade properly In a real case lets say i have

Re: [sqlalchemy] HowTo define hybridproperty.expressions referencing relationship attributes

2020-12-17 Thread Gmoney
Actually ignore that last message. I see why that case is working for us now... we are introducing the 'Tire' join explicitly for another reason in the case I was thinking of, so that .expression accidentally (on our part) just happens to work. If I just query for unicycle.weight_of_tires by

Re: [sqlalchemy] HowTo define hybridproperty.expressions referencing relationship attributes

2020-12-17 Thread Gmoney
I think I follow you... definitely about how it would be done in SQL and if constructing a query manually. I think I wasn't fully clear about what level of dynamic query building an .expression could drive. If I'm understanding correctly, it sounds like in our existing simpler case, we may just

Re: [sqlalchemy] HowTo define hybridproperty.expressions referencing relationship attributes

2020-12-17 Thread Mike Bayer
this kind of issue should be approached by thinking in SQL. the reason one wants to use a hybrid property at the class level is so that one could say: session.query(Motorcycle).filter(Motorcycle.weight_of_tires > 30) OK. So what SQL would be needed for that to work?it's actually not very

[sqlalchemy] HowTo define hybridproperty.expressions referencing relationship attributes

2020-12-17 Thread Gmoney
Trying to understand the right way to define a hybridproperty.expression that references an 'sub-attribute' of a relationship attribute. I have done it in another case by using the class name of the relationship attribute (Tire.weight vs. cls.tire.weight) and that's worked OK. But it begs the