Giter Site home page Giter Site logo

soft_delete's Introduction

Soft Delete

https://gorm.io/docs/delete.html#Soft-Delete

import "gorm.io/plugin/soft_delete"

type User struct {
  ID        uint
  Name      string
  DeletedAt soft_delete.DeletedAt
}

// Query
SELECT * FROM users WHERE deleted_at = 0;

// Delete
UPDATE users SET deleted_at = /* current unix second */ WHERE ID = 1;

Specify Time Unit

We now support ms and ns timestamp when filling the deleted_at col, only need to specify tag gorm:"softDelete:milli" or gorm:"softDelete:nano".

type User struct {
  ID    uint
  Name  string
  DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"`
  // DeletedAt soft_delete.DeletedAt `gorm:"softDelete:nano"`
}

// Query
SELECT * FROM users WHERE deleted_at = 0;

// Delete
UPDATE users SET deleted_at = /* current unix milli second or nano second */ WHERE ID = 1;

Flag Mode

flag mode will use 0, 1 to mark data as deleted or not, 1 means deleted

type User struct {
  ID    uint
  Name  string
  IsDel soft_delete.DeletedAt `gorm:"softDelete:flag"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1 WHERE ID = 1;

Mixed Mode

mixed mode will use 0, 1 to mark data as deleted or not, 1 means deleted, and store delete time

type User struct {
  ID        uint
  Name      string
  DeletedAt time.Time
  IsDel     soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1, deleted_at = /* current unix second */ WHERE ID = 1;

support mixed mode specify time unit, e.g. gorm:"softDelete:flag,DeletedAtField:DeletedAt" or gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli" or gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:nano".

type User struct {
  ID        uint
  Name      string
  DeletedAt int64
  IsDel     soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1, deleted_at = /* current unix milli second second*/ WHERE ID = 1;

soft_delete's People

Contributors

chyroc avatar davexpro avatar jinzhu avatar li-jin-gou avatar liweitingwt avatar mvn-sonnguyen-h-hn avatar qqxhb avatar shyaminayesh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

soft_delete's Issues

Flag mode as bool instead of unit

Describe the feature

My model has a bool column to indicate the soft-delete, not a uint.
It will be great if this plugin can support bool flag.

Motivation

Trying this plugin raises error:

"sql: Scan error on column index 7, name \"Deleted\": converting driver.Value type bool (\"false\") to a uint: invalid syntax"

when reading from DB bool column to model soft_delete.DeletedAt field.

Related Issues

Insert error when using 3rd way (Both DeletedAt and IsDel)

Your Question

When I using the 3rd way which contains both "DeletedAt" and "IsDel" column, I will get error when execute insert action.

model struct like:
type Model struct {
...
DeletedAt time.Time
IsDel soft_delete.DeletedAt gorm:"softDelete:flag,DeletedAtField:DeletedAt"
...
}

and when I call insert statement, I got error like that:

GORM LOG INSERT INTO test (created_at,updated_at,deleted_at,is_del) VALUES ('2021-07-06 12:01:04','2021-08-02 11:19:45.933','0000-00-00 00:00:00','0'), Error: Error 1292: Incorrect datetime value: '0000-00-00' for column 'deleted_at' at row 1

In Mysql DB, the "deleted_at" column is set to "datetime", and "is_del" is set to "int unsigned"

I guess when I set the "DeletedAtField" tag, it will auto set "DeletedAt" to "0000-00-00 00:00:00", but how do I fix this issue? is there any other tag or should I use gorm default value to solve this?

soft_delete condition can't applied to count() ๏ผŸ

Description

when i call count()
db.WithContext(ctx).Table(p.TableName()).Debug().Count(&total)
debug the sql output:
SELECT count(*) FROM project
the deleted_at where condition is not append it.

when I call find(), it will auto append

Is this a bug or is the design just like that?

Different curTime with gorm.DeletedAt

Why using different gorm.DeletedAt

See code in line https://github.com/go-gorm/gorm/blob/master/soft_delete.go#L108
And https://github.com/go-gorm/soft_delete/blob/master/soft_delete.go#L91

The document you expected this should be explained

I have some issues using this plugin with mysql, i got Error 1292: Incorrect datetime value: '1618374894' for column 'deleted_at' at row 2.

It because mysql rejected format

UPDATE `user` SET `is_deleted`=1,`deleted_at`=1618374894 
WHERE uid IN ('c1q18tjlmtapueo48tr0','c1r6jln2hraij33e5t90','c1r6ql72hraitu6jb5dg','c1r6rln2hraitu6jb5fg','c1r6vq72hraj37cr5p50') 
AND `user`.`is_deleted` = 0

Expected answer

Why dont you use same format like gorm.DeletedAt?

About flag soft_delete

Using flag to soft_delete: 1 is deleted, and 0 is active record.
Can I use 1 is actived AND 2 is deleted?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.