Skip to content

DataStore cascade delete fails for some hasOne/belongsTo relationships #11798

Open
@svidgen

Description

@svidgen

Before opening, please confirm:

JavaScript Framework

Not applicable

Amplify APIs

DataStore

Amplify Categories

api

Environment information

# Put output below this line
  System:
    OS: macOS 12.6.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 557.39 MB / 32.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.20.0 - ~/.nvm/versions/node/v16.20.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.20.0/bin/yarn
    npm: 8.19.4 - ~/.nvm/versions/node/v16.20.0/bin/npm
  Browsers:
    Chrome: 115.0.5790.170
    Safari: 16.5.2
  npmPackages:
    @testing-library/jest-dom: ^5.14.1 => 5.17.0 
    @testing-library/react: ^12.0.0 => 12.1.5 
    @testing-library/user-event: ^13.2.1 => 13.5.0 
    @types/jest: ^27.0.1 => 27.5.2 (29.5.3)
    @types/node: ^17.0.12 => 17.0.45 (20.5.0)
    @types/react: ^17.0.18 => 17.0.64 (18.2.20)
    @types/react-dom: ^17.0.9 => 17.0.20 
    @types/react-router-dom: ^5.1.8 => 5.3.3 
    @types/uuid: ^8.3.1 => 8.3.4 
    aws-amplify: unstable => 5.3.7 
    ini: ^2.0.0 => 2.0.0 (1.3.8)
    inquirer: ^8.2.0 => 8.2.6 
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    react-jss: ^10.7.1 => 10.10.0 
    react-router-dom: ^5.2.0 => 5.3.4 
    react-scripts: 5.0.0 => 5.0.0 
    typescript: ^4.3.5 => 4.9.5 
    ulid: ^2.3.0 => 2.3.0 
    web-vitals: ^2.1.0 => 2.1.4 
    yarn: ^1.22.11 => 1.22.19 
  npmGlobalPackages:
    @aws-amplify/cli: 12.1.1
    corepack: 0.17.0
    envinfo: 7.10.0
    figlet: 1.6.0
    npm: 8.19.4
    yarn: 1.22.19

Describe the bug

It looks like a @hasOne with a corresponding @belongsTo causes DataStore's delete cascade to break.

Expected behavior

Cascade delete on @hasOne relationships should work the same regardless of whether a corresponding @belongsTo exists.

Reproduction steps

Add these models to your schema:

# 1.2 Has one - unidirectional
type Project11 @model {
	customProjectId: ID! @primaryKey(sortKeyFields: ["name"])
	name: String!
	description: String!
	rating: Int
	team: Team11 @hasOne
}

type Team11 @model {
	customTeamId: ID! @primaryKey(sortKeyFields: ["name"])
	name: String!
	description: String!
	rating: Int
}

# 1.2 Has one - bidirectional
type Project12 @model {
	customProjectId: ID! @primaryKey
	name: String!
	description: String!
	rating: Int
	team: Team12 @hasOne
}

type Team12 @model {
	customTeamId: ID! @primaryKey
	name: String!
	description: String!
	rating: Int
	project: Project12 @belongsTo
}

# 1.2.b Has one - bidirectional
type Project13 @model {
	customId: ID! @primaryKey(sortKeyFields: ["name"])
	name: String!
	description: String!
	rating: Int
	team: Team13 @hasOne
}

type Team13 @model {
	customId: ID! @primaryKey(sortKeyFields: ["name"])
	name: String!
	description: String!
	rating: Int
	project: Project13 @belongsTo
}

Attempt to delete the parent (Project) record. Note that in the first case, the child (Team) is deleted. In the "bidirectional" cases, only the parent record is deleted.

During repro, I inserted these lines to storage/adapter/StorageAdapterBase.ts before line 523.

logger.debug('cascading delete', {
	from: model,
	to: {
		model: r.remoteModelConstructor.name,
		queryObject,
	},
});

I found that DataStore attempts to cascade the deletion, but uses the wrong condition:

image

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

Metadata

Metadata

Assignees

Labels

DataStoreRelated to DataStore categorybugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions