librelist archives

« back to archive

How to delete more than one entity in flask-sqlalchemy

How to delete more than one entity in flask-sqlalchemy

From:
Alex
Date:
2011-05-28 @ 05:43
Hi,
I came across a question about sqlalchemy.
db.session.delete() handles one object one time.
How to delete more than one object in one query.
thanks.

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Armin Ronacher
Date:
2011-05-28 @ 12:28
Hi,

Also possible:

Model.query.filter(Model.id.in_([1, 2, 3, 4])).delete()


Regards,
Armin

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Ron DuPlain
Date:
2011-05-28 @ 05:51
On Sat, May 28, 2011 at 1:43 AM, Alex <gfreezy@gmail.com> wrote:
> Hi,
> I came across a question about sqlalchemy.
> db.session.delete() handles one object one time.
> How to delete more than one object in one query.
> thanks.
>

See cascading delete topic at
http://www.sqlalchemy.org/docs/06/orm/tutorial.html#deleting

-Ron

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Alex
Date:
2011-05-28 @ 06:16
Hi Ron,
Thanks for replying first.
But that's not what i want.

For example,
I have two records:
record1 = ['1', 'hello', 'tom', '2010-08-09']
record2 = ['2', 'haha', 'allen', '2010-09-12']
records=[record1,record2]
I want to delete the two records in one operation.
Is there some method than can achive the following function
'db.session.deletes(records)'
SQLAlchemy implements 'delete' that can delete one record a time.


2011/5/28 Ron DuPlain <ron.duplain@gmail.com>

> On Sat, May 28, 2011 at 1:43 AM, Alex <gfreezy@gmail.com> wrote:
> > Hi,
> > I came across a question about sqlalchemy.
> > db.session.delete() handles one object one time.
> > How to delete more than one object in one query.
> > thanks.
> >
>
> See cascading delete topic at
> http://www.sqlalchemy.org/docs/06/orm/tutorial.html#deleting
>
> -Ron
>

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Daniel , Dao Quang Minh
Date:
2011-05-28 @ 09:28
You can delete multiple objects separately and flush/commit only once
On May 28, 2011 2:16 PM, "Alex" <gfreezy@gmail.com> wrote:
> Hi Ron,
> Thanks for replying first.
> But that's not what i want.
>
> For example,
> I have two records:
> record1 = ['1', 'hello', 'tom', '2010-08-09']
> record2 = ['2', 'haha', 'allen', '2010-09-12']
> records=[record1,record2]
> I want to delete the two records in one operation.
> Is there some method than can achive the following function
> 'db.session.deletes(records)'
> SQLAlchemy implements 'delete' that can delete one record a time.
>
>
> 2011/5/28 Ron DuPlain <ron.duplain@gmail.com>
>
>> On Sat, May 28, 2011 at 1:43 AM, Alex <gfreezy@gmail.com> wrote:
>> > Hi,
>> > I came across a question about sqlalchemy.
>> > db.session.delete() handles one object one time.
>> > How to delete more than one object in one query.
>> > thanks.
>> >
>>
>> See cascading delete topic at
>> http://www.sqlalchemy.org/docs/06/orm/tutorial.html#deleting
>>
>> -Ron
>>

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Ron DuPlain
Date:
2011-05-28 @ 06:23
On Sat, May 28, 2011 at 2:16 AM, Alex <gfreezy@gmail.com> wrote:
> I have two records:
> record1 = ['1', 'hello', 'tom', '2010-08-09']
> record2 = ['2', 'haha', 'allen', '2010-09-12']
> records=[record1,record2]
> I want to delete the two records in one operation.
> Is there some method than can achive the following function
> 'db.session.deletes(records)'
> SQLAlchemy implements 'delete' that can delete one record a time.

That's the SQLAlchemy session API: delete but not deletes.  Simply:

    for record in records:
        db.session.delete(record)
    db.session.commit()

With SQLAlchemy sessions, the delete doesn't hit the database until a
commit, so there's no problem in issuing one delete at a time.

-Ron

Re: [flask] How to delete more than one entity in flask-sqlalchemy

From:
Alex
Date:
2011-05-28 @ 06:54
Thank you, Ron. I got it.

2011/5/28 Ron DuPlain <ron.duplain@gmail.com>

> On Sat, May 28, 2011 at 2:16 AM, Alex <gfreezy@gmail.com> wrote:
> > I have two records:
> > record1 = ['1', 'hello', 'tom', '2010-08-09']
> > record2 = ['2', 'haha', 'allen', '2010-09-12']
> > records=[record1,record2]
> > I want to delete the two records in one operation.
> > Is there some method than can achive the following function
> > 'db.session.deletes(records)'
> > SQLAlchemy implements 'delete' that can delete one record a time.
>
> That's the SQLAlchemy session API: delete but not deletes.  Simply:
>
>    for record in records:
>        db.session.delete(record)
>    db.session.commit()
>
> With SQLAlchemy sessions, the delete doesn't hit the database until a
> commit, so there's no problem in issuing one delete at a time.
>
> -Ron
>