edgedb / easy-edgedb

An illustrated textbook designed to be a one-stop shop for learning EdgeDB
https://www.edgedb.com/easy-edgedb
76 stars 39 forks source link

Unable to delete City in Chapter 3 #118

Closed 0c12a4c9-4c00-4211-8531-353dbff93969 closed 1 year ago

0c12a4c9-4c00-4211-8531-353dbff93969 commented 1 year ago

When running this command delete City filter .id = <uuid>'d1e38192-0bd6-11ee-ba45-d7ecb20723cf'; (obviously with the uuid from my database) I get the following error:

ConstraintViolationError: deletion of default::City (1ec8bc46-39f1-11ee-b6b6-abc6048567b0) is prohibited by link target policy
Details: Object is still referenced in link places_visited of default::Person (8d9878e0-39fc-11ee-bfac-0fce9c022e13).

My schema is this:

module default {
    abstract type Person {
        required name: str;
        multi places_visited: Place;
        age: HumanAge;
    }

    type PlayerCharacter extending Person {
        required class: Class;
    }

    type NonPlayerCharacter extending Person;

    type Vampire extending Person;

    abstract type Place {
        required name: str;
        modern_name: str;
        important_places: array<str>;
    }

    type City extending Place;

    type Country extending Place;

    scalar type Class extending enum<Rogue, Mystic, Merchang>;

    scalar type HumanAge extending int16 {
        constraint max_value(120);
    }
}

select City {*} filter .name = "Bistritz" returns

[
  {
    "name": "Bistritz",
    "id": "1ec8bc46-39f1-11ee-b6b6-abc6048567b0",
    "important_places": null,
    "modern_name": "Bistrița"
  },
  {
    "name": "Bistritz",
    "id": "0bc9a138-3a18-11ee-91f1-874678e6f85c",
    "important_places": [
      "Golden Krone Hotel"
    ],
    "modern_name": "Bistrița"
  }
]

I copied the above code from the edgedb ui which replaced {} with null for some reason. That's when I use the copy button.

Not sure what the problem is here. I guess I mistyped something?

Dhghomon commented 1 year ago

Hmm, I'll take a closer look at this tomorrow to see if I messed up the insertion order and let you know once it's fixed if that's the case. Otherwise to quickly resolve the issue you could quickly delete the Person objects that link to cities and then delete the City and reinsert, or if feeling a bit more ambitious (since it's from a later chapter) you could change the deletion policy and do a migration which will then let you delete the City even if it's being linked to:

https://github.com/edgedb/easy-edgedb/blob/master/chapter13/index.md#on-target-delete-on-source-delete

0c12a4c9-4c00-4211-8531-353dbff93969 commented 1 year ago

Most likely I didn't follow the tutorial to the letter, that's my guess. If it helps, the player character had the old Bistritz linked.

It's interesting that there's no inverse relationship from Place back to Person { places_visited} to select persons that visited a place, but maybe that comes at a later point in the tutorial.

As an additional note: the outputs for select City { ... }; in chapter 3 show only one Bistritz in the code snippets on the website while someone who followed the tutorial sees Bistritz two times in the output of their local DB (since deleting happens in the last step).

0c12a4c9-4c00-4211-8531-353dbff93969 commented 1 year ago

I solved the problem by unlinking the old Bistritz entry through the edgedb ui

Dhghomon commented 1 year ago

It does indeed come up later, that's known as a backlink and it shows up in Chapter 14. It's cool to see that you're already feeling a need for something that does get addressed later in the book.

Dhghomon commented 1 year ago

Looks like it wasn't just you! At this point in the book the reader doesn't know how to modify deletion policies or update objects yet so the solution is to not give the PC a link to the City objects and then to temporarily delete Jonathan Harker (along with some notes that this process gets smoother later on when the reader knows more).

https://github.com/edgedb/easy-edgedb/commit/23e5db1811308a340b2f61ffeea597ac32292b6e

0c12a4c9-4c00-4211-8531-353dbff93969 commented 1 year ago

Awesome, thanks a lot