librelist archives

« back to archive

sqlalchemy: deleting objects

sqlalchemy: deleting objects

From:
Cornelius Kölbel
Date:
2014-11-13 @ 20:38
Hi,

assume I have a Object "User" in my models.py.

I understand, that I can do such things:

    nasty_folk = User.query.filter_by(name="unpleasant guy")

I would like to delete all those guys. Is there a way to do this
directly in the Object notation?
Or do I always have to use the db object and the session directly

    for jerk in nasty_folks:
        db.session.delete(jerk)
    db.session.submit()

Kind regards
Cornelius

Re: [flask] sqlalchemy: deleting objects

From:
Jack Maney
Date:
2014-11-13 @ 21:30
Well, you could pass along the SQLAlchemy database object into the User
object, and then use that in a delete method...

class User(db.Model):

    def __init__(self, db):
        db.Model.__init__(self)
        self.db = db

    def delete(self):
        self.db.session.delete(self)
        db.session.submit()

...however, you might want to take care not to do anything to a user object
after the delete() method has been called and before the user object has
been garbage collected.

What would probably be a better idea is to have a function (possibly tied
to a route, possibly not) that finds the nasty_folks and goes through the
`for jerk in nasty_folks` loop for you.

On Thu, Nov 13, 2014 at 2:38 PM, Cornelius Kölbel <cornelius@privacyidea.org
> wrote:

> Hi,
>
> assume I have a Object "User" in my models.py.
>
> I understand, that I can do such things:
>
>     nasty_folk = User.query.filter_by(name="unpleasant guy")
>
> I would like to delete all those guys. Is there a way to do this
> directly in the Object notation?
> Or do I always have to use the db object and the session directly
>
>     for jerk in nasty_folks:
>         db.session.delete(jerk)
>     db.session.submit()
>
> Kind regards
> Cornelius
>
>


-- 
"Structures are the weapons of the mathematician."
--Bourbaki

Re: [flask] sqlalchemy: deleting objects

From:
David Baumgold
Date:
2014-11-14 @ 03:06
You’re also better off deleting all the objects in one SQL query, instead 
of one query per object. Fortunately, it’s easy! Instead of:

for jerk in nasty_folk:
    db.session.delete(jerk)

Just do:

nasty_folk.delete()

Much simpler, *and* better performance to boot!

David Baumgold

On November 13, 2014 at 4:32:36 PM, Jack Maney (jackmaney@gmail.com) wrote:

Well, you could pass along the SQLAlchemy database object into the User 
object, and then use that in a delete method...

class User(db.Model):

    def __init__(self, db):
        db.Model.__init__(self)
        self.db = db
    
    def delete(self):
        self.db.session.delete(self)
        db.session.submit()

...however, you might want to take care not to do anything to a user 
object after the delete() method has been called and before the user 
object has been garbage collected.

What would probably be a better idea is to have a function (possibly tied 
to a route, possibly not) that finds the nasty_folks and goes through the 
`for jerk in nasty_folks` loop for you.

On Thu, Nov 13, 2014 at 2:38 PM, Cornelius Kölbel 
<cornelius@privacyidea.org> wrote:
Hi,

assume I have a Object "User" in my models.py.

I understand, that I can do such things:

    nasty_folk = User.query.filter_by(name="unpleasant guy")

I would like to delete all those guys. Is there a way to do this
directly in the Object notation?
Or do I always have to use the db object and the session directly

    for jerk in nasty_folks:
        db.session.delete(jerk)
    db.session.submit()

Kind regards
Cornelius




--
"Structures are the weapons of the mathematician."
--Bourbaki

Re: [flask] sqlalchemy: deleting objects

From:
Cornelius Kölbel
Date:
2014-11-14 @ 14:30
Hi David, hi Jack,
thanks a lot for both advices.
I think I go with the second one.

Kind regards
Cornelius
Am 14.11.2014 um 04:06 schrieb David Baumgold:
> You’re also better off deleting all the objects in one SQL query,
> instead of one query per object. Fortunately, it’s easy! Instead of:
>
> for jerk in nasty_folk:
>     db.session.delete(jerk)
>
> Just do:
>
> nasty_folk.delete()
>
> Much simpler, *and* better performance to boot!
>
> David Baumgold
>
> On November 13, 2014 at 4:32:36 PM, Jack Maney (jackmaney@gmail.com
> <mailto:jackmaney@gmail.com>) wrote:
>
>> Well, you could pass along the SQLAlchemy database object into the
>> User object, and then use that in a delete method...
>>
>> class User(db.Model):
>>
>>     def __init__(self, db):
>>         db.Model.__init__(self)
>>         self.db = db
>>     
>>     def delete(self):
>>         self.db.session.delete(self)
>>         db.session.submit()
>>
>> ...however, you might want to take care not to do anything to a user
>> object after the delete() method has been called and before the user
>> object has been garbage collected.
>>
>> What would probably be a better idea is to have a function (possibly
>> tied to a route, possibly not) that finds the nasty_folks and goes
>> through the `for jerk in nasty_folks` loop for you.
>>
>> On Thu, Nov 13, 2014 at 2:38 PM, Cornelius Kölbel
>> <cornelius@privacyidea.org <mailto:cornelius@privacyidea.org>> wrote:
>>
>>     Hi,
>>
>>     assume I have a Object "User" in my models.py.
>>
>>     I understand, that I can do such things:
>>
>>         nasty_folk = User.query.filter_by(name="unpleasant guy")
>>
>>     I would like to delete all those guys. Is there a way to do this
>>     directly in the Object notation?
>>     Or do I always have to use the db object and the session directly
>>
>>         for jerk in nasty_folks:
>>             db.session.delete(jerk)
>>         db.session.submit()
>>
>>     Kind regards
>>     Cornelius
>>
>>
>>
>>
>> --
>> "Structures are the weapons of the mathematician."
>> --Bourbaki