hello yacine,
elixir isn't known to reinvent sa, but please point me to things you would
change for a pure approach.
part of the lower level stuff is needed to turn foreign keys on in sqlite.
in the mean time, i did a declarative example which fails like elixir.
btw. this is the same problem you have also previously reported on this list.
alex
On 09/12/2010 09:58 AM, chaouche yacine wrote:
> hello alex,
>
> In your elixir program, you are mixing some imports from sqlalchemy
> (create_engine from example) with imports from elixir. Did you try an elixir
> only approach ?
>
> Y.Chaouche
>
>
>
> --- On Sat, 9/11/10, alex bodnaru <[email protected]> wrote:
>
>> From: alex bodnaru <[email protected]>
>> Subject: [elixir] problem with cascade deletes
>> To: [email protected]
>> Date: Saturday, September 11, 2010, 6:31 AM
>>
>> hello friends,
>>
>> there seems to be a flaw in elixir with cascade deletes.
>>
>> i have a program that does it with sqlalchemy orm, and a
>> similar one to do it
>> with elixir.
>> instead of deleting the elixir program only nulls the keys
>> in the child.
>>
>> the programs are attached.
>>
>> best regards,
>> alex
>>
>> --
>> You received this message because you are subscribed to the
>> Google Groups "SQLElixir" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/sqlelixir?hl=en.
>>
>>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlelixir?hl=en.
from sqlalchemy import (MetaData, Table, Column, Integer, String, ForeignKey,
create_engine, ForeignKeyConstraint)
from sqlalchemy.orm import scoped_session, sessionmaker, relation
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine("sqlite:///:memory:", echo=True)
connection = engine.connect()
connection.execute('PRAGMA foreign_keys = ON;')
Base = declarative_base(bind=engine)
class MyClass(Base):
__tablename__ = 'mytable'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(50))
# children = relation('MyOtherClass', backref='parent')
def __repr__(self):
return '<MyClass %s, "%s">' % ("None" if self.id is None else str(self.id), self.name)
class MyOtherClass(Base):
__tablename__ = 'myothertable'
id = Column(Integer, primary_key=True, autoincrement=True)
parent_id = Column(Integer, ForeignKey('mytable.id', ondelete="CASCADE"))
parent = relation('MyClass', backref='children', passive_deletes=True)
name = Column(String(50))
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)
Base.metadata.create_all(engine)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
alex = MyClass(name='alex')
pisi = MyClass(name='pisi')
session.add_all([
alex, pisi
])
print alex, pisi
session.flush()
session.add_all([
MyOtherClass(parent=alex, name='dagan'),
MyOtherClass(parent=alex, name='shaked'),
MyOtherClass(parent=pisi, name='dagan'),
MyOtherClass(parent=pisi, name='shaked'),
])
session.flush()
shaked1 = session.query(MyOtherClass).filter_by(parent_id=1, name=u'shaked')
session.delete(alex)
session.flush()
for my in session.query(MyClass).all():
print my
for my in session.query(MyOtherClass).all():
print my