[Dbix-class] "soft deletion"

Jesse Sheidlower jester at panix.com
Tue Feb 17 19:21:59 GMT 2009


On Mon, Feb 16, 2009 at 01:49:08PM -0600, fREW Schmidt wrote:
> Hello friends!
> 
> I am sure there is a better term for this, I just don't know what it is.  I
> would like for my database to have some type of removal field that would
> effectively mark a row as deleted without deleting it.  I've seen ORMs that
> do this before and I was wondering if DBIC did something or had support for
> something like this.
> 
> To make myself more clear, I'd like either a binary flag or a date which
> would probably be called deleted or date_deleted or is_deleted (feel free to
> give input in this) and I'd like it not to show up in regular resultsets
> unless I explicitly say I want to see the deleted rows.
> 
> Thoughts?

Other people have made what are probably more robust
suggestions.  However, what I do in my apps is very similar to
what you're asking for here:

In any table to which this applies, I have a field called
"deleted" defined as "datetime default NULL". When I want to
delete a field, I have this in my do_delete() routine:

  my $record = $c->stash->{model}->find($id); # add error-checking
  $record->deleted(\'now()');
  $record->update;

Then, in my ResultSet class I just have this:

# only find non-deleted records
__PACKAGE__->resultset_attributes({ where => { deleted => undef } });

Presto, any normal search, find, etc. operation will always
only find the non-deleted records; I don't have to think about
the fact that the "deleted" ones are still in the database,
just with a flag set. It's transparent to the user.

I could have sworn that I wrote this into a cookbook entry,
but I can't find it now.

Jesse Sheidlower



More information about the DBIx-Class mailing list