Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

How to set on delete cascade in .graphql file #2277

Open
rrguhan6 opened this issue Mar 14, 2021 · 3 comments
Open

How to set on delete cascade in .graphql file #2277

rrguhan6 opened this issue Mar 14, 2021 · 3 comments
Labels
question Further information is requested

Comments

@rrguhan6
Copy link

rrguhan6 commented Mar 14, 2021

I'm using postgres 11.10

My model is

""" 
@model
"""
type Note  {
  """ @id """
  id: ID!
  title: String!
  description: String
   """
  @oneToMany(field: 'note')
  """
  comments: [Comment]
}

""" 
@model(delete: true)
 """
type Comment {
  """ @id """
  id: ID!
  text: String
  description: String
}

type Query {
  getDraftNotes: [Note]
}

Query :

mutation{
  deleteNote(input:{id:1}){
    id
    title
    description
  }
}

output:

{
  "errors": [
    {
      "message": "delete from \"note\" where \"id\" = $1 returning * - update or delete on table \"note\" violates foreign key constraint \"comment_noteid_foreign\" on table \"comment\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "deleteNote"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "length": 274,
          "name": "error",
          "severity": "ERROR",
          "code": "23503",
          "detail": "Key (id)=(1) is still referenced from table \"comment\".",
          "schema": "public",
          "table": "comment",
          "constraint": "comment_noteid_foreign",
          "file": "ri_triggers.c",
          "line": "2797",
          "routine": "ri_ReportViolation",
          "stacktrace": [
            "error: delete from \"note\" where \"id\" = $1 returning * - update or delete on table \"note\" violates foreign key constraint \"comment_noteid_foreign\" on table \"comment\"",
            "    at Parser.parseErrorMessage (/home/guhan/code/gql-postgress/web/node_modules/pg-protocol/src/parser.ts:357:11)",
            "    at Parser.handlePacket (/home/guhan/code/gql-postgress/web/node_modules/pg-protocol/src/parser.ts:186:21)",
            "    at Parser.parse (/home/guhan/code/gql-postgress/web/node_modules/pg-protocol/src/parser.ts:101:30)",
            "    at Socket.stream.on (/home/guhan/code/gql-postgress/web/node_modules/pg-protocol/src/index.ts:7:48)",
            "    at Socket.emit (events.js:198:13)",
            "    at Socket.EventEmitter.emit (domain.js:448:20)",
            "    at addChunk (_stream_readable.js:288:12)",
            "    at readableAddChunk (_stream_readable.js:269:11)",
            "    at Socket.Readable.push (_stream_readable.js:224:10)",
            "    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)"
          ]
        }
      }
    }
  ],
  "data": {
    "deleteNote": null
  }
}

pls help me to resolve this

@rrguhan6 rrguhan6 added the question Further information is requested label Mar 14, 2021
@wtrocki
Copy link
Contributor

wtrocki commented Mar 14, 2021

I think we do not enable cascade deletes as form of precaution, but I get that this is inconvenient to delete all comments from end user app to delete post

Anyone can alter cascade rules on FK but that is like a lot of work if you have large db and on the other hand I do not want to have issue ala graphback deleted half of my database because we setup default cascades.

We need new annotations/ability probably so this is feature request IMHO

@wtrocki
Copy link
Contributor

wtrocki commented Mar 14, 2021

Actually I cannot replicate this. I see we do not put any so constraints by default. More info needed

@rrguhan6
Copy link
Author

Actually I cannot replicate this. I see we do not put any so constraints by default. More info needed

Flow to reproduce this issue

  1. create a note record
    Query:
mutation {
  createNote(input: { title: "the gql 2 !" }) {
    id
    title
  }
}

Output:

{
  "data": {
    "createNote": {
      "id": "2",
      "title": "the gql 2 !"
    }
  }
}

2.create a comment with noteId
query:

mutation {
  createComment(input: { noteId: 2, text: "wow " }) {
    id
    text
    note {
      id
      title
    }
  }
}

output:

{
  "data": {
    "createComment": {
      "id": "2",
      "text": "wow",
      "note": {
        "id": "2",
        "title": "the gql 2 !"
      }
    }
  }
}

3.Now delete the note record
query:

mutation {
  deleteNote(input: { id: 2 }) {
    id
    title
  }
}

output:

{
  "errors": [
    {
      "message": "delete from \"note\" where \"id\" = $1 returning * - update or delete on table \"note\" violates foreign key constraint \"comment_noteid_foreign\" on table \"comment\"",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "deleteNote"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "length": 274,
          "name": "error",
          "severity": "ERROR",
          "code": "23503",
          "detail": "Key (id)=(2) is still referenced from table \"comment\".",
          "schema": "public",
          "table": "comment",
          "constraint": "comment_noteid_foreign",
          "file": "ri_triggers.c",
          "line": "2797",
          "routine": "ri_ReportViolation",
          "stacktrace": [
            "error: delete from \"note\" where \"id\" = $1 returning * - update or delete on table \"note\" violates foreign key constraint \"comment_noteid_foreign\" on table \"comment\"",
            "    at Parser.parseErrorMessage (/home/guhan/code/gql-postgress/web3/node_modules/pg-protocol/src/parser.ts:357:11)",
            "    at Parser.handlePacket (/home/guhan/code/gql-postgress/web3/node_modules/pg-protocol/src/parser.ts:186:21)",
            "    at Parser.parse (/home/guhan/code/gql-postgress/web3/node_modules/pg-protocol/src/parser.ts:101:30)",
            "    at Socket.stream.on (/home/guhan/code/gql-postgress/web3/node_modules/pg-protocol/src/index.ts:7:48)",
            "    at Socket.emit (events.js:198:13)",
            "    at Socket.EventEmitter.emit (domain.js:448:20)",
            "    at addChunk (_stream_readable.js:288:12)",
            "    at readableAddChunk (_stream_readable.js:269:11)",
            "    at Socket.Readable.push (_stream_readable.js:224:10)",
            "    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)"
          ]
        }
      }
    }
  ],
  "data": {
    "deleteNote": null
  }
}

I am using postgres - 11.10 , node - v10.19.0 and created graphql project using "npm init graphback" command.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants