Hello everyone!

In my application, I have a class Product that can belong to a
Category. (1 product, 1 category). The category knows which products
belong to it thanks to a backref. A product can not exist if
it doesn't belong to a category. If a category is deleted, all it's
products are deleted as well. That's why I would like to stablish a
"delete-orphan" in that relationship.Something like:

class Product(declarative_base):
        __tablename__ = "products"
        _id = Column("id", Integer, primary_key=True)

        _model = Column("model", Unicode(128))
        _categoryId = Column("category_id", Integer,
                          ForeignKey("categories.id"), key="categoryId")
        _category = relationship("Category",
                                        uselist=False,
                                        backref=backref(
                                                     "_products",
                                                     collection_class=set,
                                                     #Set up delete
orphans here?
                                                )                               
        
                                        )

class Category(declarative_base):
        __tablename__ = "categories"
        _id = Column("id", Integer, primary_key=True)
        _name = Column("name", String(50))
        #_products > Backref from Product class

The problem is that, in a certain spot I want to create a category and
a product in one shot. This is running in a web server, that may
receive a category looking more or less like this:

#This is a new category with a new product inside. They don't have id,
# therefore, both of them are considered "new"
{
    "_name": "cellphones",
    "_products": [
        {
            "_model": "iphone"
        }
    ]
}

That data (is JSON) is sent to the Category "handler". That handler
does the following
1) Creates a new Category() instance,
2) Fill the non-relationship fields ("_name" in this case)
3) Adds the category to the session, so it gets an _id
4) Go through the relationships fields ("_products")
5) If there's a dictionary inside, call recursively to the same method
with that dictionary.
6) The recursive call will try to create a new product (with the
proper "_model") and TRIES to add it to the database
7) The recursive call returns, so that newly created product can be
added to the "_products" relationship of the category and the backref
will properly set up the "_category" relationship in the product

And in the 6th point is where my problem shows up: If I set up the
delete-orphans, the database detects that I'm trying to insert a
product that doesn't belong to a category, therefore, it's orphan...
and blows up.

Is there a way to delay the triggering of the "delete-orphans" or...
or something similar, so I can have a product not belonging to a
category... for a bit (until the category has finished loading?)

I can start digging the information the "_products" relationship has
inside, checking the "parent", the "backref" attribute, pass the
category to the recursive method, so it will put it in the
Product._category field,... yadda, yadda, yadda, but I was wondering
if there's a better way of achieving what I want (assuming I explained
properly what I want)

Thank you in advance!

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