On 09/13/2010 05:49 PM, Michael Bayer wrote:
>
> On Sep 13, 2010, at 11:16 AM, alex bodnaru wrote:
>
>> hope my approach isn't too simplist, but onetomany is usually implemented in
>> rdbms by an manytoone column or a few of them, with or without ri clauses: 
>> thus,
>> a foreign key or an index.
>> conversely, a manytoone relation has an implicit onetomany one (or an 
>> explicit
>> onetoone).
>
> if you read what I wrote, I was explaining, that we architecturally choose not
> to generate the implicit reverse direction when it isn't specified by the
> user.  And that this decision is not too controversial since Hibernate made
> the same one.
>
>>
>> the example i've given with elixir (look at the sql echo) shows the onetomany
>> updates the foreign key to null, not knowing they wouldn't be found in the
>> cascading delete. i'm searching the exact point elixir should give the
>> passive_deletes to the sa relation, thus to force it to give it to the right
>> side of it.
>
> right  - Elixir has a more abstracted layer of user configuration, which is
> basically what you're asking SQLAlchemy to build into it.  Id rather make
> things simpler on the inside, not more magical. 
> -- 
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@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.
well i found the problem (it's in my code). i was hilariously dealing with
something more magical in sa then in elixir: i've used backref from sa instead
of inverse from elixir. for this specific case it's simply my error to do this,
since passive_deletes is an argument to be passed to an existing relation, but
the magic i usually wanted to achieve with backref was auto-creating a onetomany
relation to complement a manytoone one, especially when i don't wish to touch
the file of the parent entity.
btw, to achieve this same magic with elixir i've made in the past an elixir
extension, that was rejected by elixir people, that pointed me to backref.

best regards and thanks again,
alex

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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.

from sqlalchemy import (MetaData, Table, Column, Integer, String, ForeignKey,
                        create_engine, ForeignKeyConstraint)
from sqlalchemy.orm import (mapper, relationship, sessionmaker)

from elixir import *
from sqlalchemy.interfaces import PoolListener

class SQLiteFKListener(PoolListener):
    def connect(self, dbapi_con, con_record):
        dbapi_con.execute('PRAGMA foreign_keys = ON;')

engine = create_engine("sqlite:///:memory:", echo=True, listeners=[SQLiteFKListener()])

metadata.bind = engine


class MyClass(Entity):
    using_options(tablename='mytable')
    id = Field(Integer, primary_key=True, autoincrement=True)
    name = Field(String(20))
    children = OneToMany('MyOtherClass', passive_deletes=True)

    def __repr__(self):
        return '<MyClass %s, "%s">' % ("None" if self.id is None else str(self.id), self.name)

class MyOtherClass(Entity):
    using_options(tablename='myothertable')
    id = Field(Integer, primary_key=True, autoincrement=True)
    name = Field(String(20))
    parent = ManyToOne('MyClass', inverse='children', colname='parent_id', ondelete="cascade")

    def __repr__(self):
        return '<MyOtherClass %s, %s, "%s">' % ("None" if self.parent_id is None else str(self.parent_id), "None" if self.id is None else str(self.id), self.name)


setup_all()
create_all()


alex = MyClass(name='alex')
pisi = MyClass(name='pisi')

print alex, pisi

#session.commit()
session.flush()

print alex, pisi

alexdagan = MyOtherClass(parent=alex, name='dagan')
alexshaked = MyOtherClass(parent=alex, name='shaked')
pisidagan = MyOtherClass(parent=pisi, name='dagan')
pisishaked = MyOtherClass(parent=pisi, name='shaked')

#session.commit()
session.flush()

shaked1 = session.query(MyOtherClass).filter_by(parent_id=1, name=u'shaked')

session.delete(alex)

#session.commit()
session.flush()


for my in session.query(MyClass).all():
    print my
for my in session.query(MyOtherClass).all():
    print my

Reply via email to