[#1220] fixed field column name conflict on cascade record deletion

This commit is contained in:
Gani Georgiev
2022-12-09 19:09:43 +02:00
parent 59b41c8202
commit 9c7f48a66e
5 changed files with 34 additions and 22 deletions

View File

@@ -379,6 +379,7 @@ func (dao *Dao) DeleteRecord(record *models.Record) error {
// check if related records has to be deleted (if `CascadeDelete` is set)
// OR
// just unset the record id from any relation field values (if they are not required)
uniqueJsonEachAlias := "__je__" + security.PseudorandomString(5)
for refCollection, fields := range refs {
for _, field := range fields {
options, _ := field.Options.(*schema.RelationOptions)
@@ -387,16 +388,16 @@ func (dao *Dao) DeleteRecord(record *models.Record) error {
// fetch all referenced records
recordTableName := inflector.Columnify(refCollection.Name)
fieldColumnName := inflector.Columnify(field.Name)
prefixedFieldName := recordTableName + "." + inflector.Columnify(field.Name)
err := txDao.RecordQuery(refCollection).
Distinct(true).
LeftJoin(fmt.Sprintf(
// note: the case is used to normalize value access for single and multiple relations.
`json_each(CASE WHEN json_valid([[%s]]) THEN [[%s]] ELSE json_array([[%s]]) END) as {{__je__}}`,
fieldColumnName, fieldColumnName, fieldColumnName,
`json_each(CASE WHEN json_valid([[%s]]) THEN [[%s]] ELSE json_array([[%s]]) END) as {{%s}}`,
prefixedFieldName, prefixedFieldName, prefixedFieldName, uniqueJsonEachAlias,
), nil).
AndWhere(dbx.Not(dbx.HashExp{recordTableName + ".id": record.Id})).
AndWhere(dbx.HashExp{"__je__.value": record.Id}).
AndWhere(dbx.HashExp{uniqueJsonEachAlias + ".value": record.Id}).
All(&rows)
if err != nil {
return err