Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationship constraint returns wrong object, no relationship loaded #792

Open
lenormf opened this issue Dec 3, 2022 · 1 comment
Open

Comments

@lenormf
Copy link

lenormf commented Dec 3, 2022

Describe the bug

Pretty sure I’m missing something, would like to understand what.

Getting (checking for existence of) a relationship using the related object’s id returns the wrong parent object, doesn’t load the relationship either.

Steps to reproduce the bug

  1. Models
class BoardItem extends Model {
    static entity = "board_item"

    static fields() {
        return {
            id: this.uid(),
            board_id: this.uid(),
            highlight_board_item_id: this.uid(),
            board_item_id: this.attr(null),
            board_item_type: this.attr(null),
            board_item_impl: this.morphTo("board_item_id", "board_item_type"),
            index: this.number(0),
        }
    }
}

class HighlightBoardItem extends Model {
    static entity = "highlight_board_item"

    static fields() {
        return {
            id: this.uid(),
            highlight_board_id: this.uid(),
            board_item: this.hasOne(BoardItem, "highlight_board_item_id"),
            index: this.number(0),
        }
    }
}
  1. State of the store

board_item

[
  {
    "$id": "$uid10",
    "id": "$uid10",
    "board_id": "$uid3",
    "highlight_board_item_id": "$uid21",
    "board_item_id": "$uid8",
    "board_item_type": "board_item_value",
    "board_item_impl": [snip],
    "index": 0
  },
  {
    "$id": "$uid14",
    "id": "$uid14",
    "board_id": "$uid3",
    "highlight_board_item_id": "$uid15",
    "board_item_id": "$uid12",
    "board_item_type": "board_item_value",
    "board_item_impl": [snip],
    "index": 1
  }
]

highlight_board_item

[
  {
    "$id": "$uid21",
    "id": "$uid21",
    "highlight_board_id": "$uid7",
    "board_item": {
      "$id": "$uid10",
      "id": "$uid10",
      "board_id": "$uid3",
      "highlight_board_item_id": "$uid21",
      "board_item_id": "$uid8",
      "board_item_type": "board_item_value",
      "board_item_impl": [snip],
      "index": 0
    },
    "index": 0
  }
]
  1. Code

Using the ID of a board_item that is not highlighted (i.e. assigned to the 1:1 relationship), but in the database nonetheless.

this.board_item.id: $uid14

this.repoHighlightBoardItem.query().with("board_item", query => {
    query.whereId(this.board_item.id)
}).get()

Ultimately I want the .get() above to be turned into .exists(), which would return True in the above.

  1. Outcome
[
  {
    "$id": "$uid21",
    "id": "$uid21",
    "highlight_board_id": "$uid7",
    "board_item": null,
    "index": 0
  }
]

The above object corresponds to the one that is linked to the highlighted item, and does not have its predicate relationship loaded anyway.

The following should be working a priori, but doesn’t:

const repoHighlightBoardItem = this.$store.$db().model("highlight_board_item")
return this.repoHighlightBoardItem.query().with("board_item", query => {
     query.whereId(this.board_item.id)
}).has("board_item").exists()

Horrible workaround:

const repoBoardItem = this.$store.$db().model("board_item")
return repoBoardItem.query().whereId(this.board_item.id).where("highlight_board_item_id", hbii => this.repoHighlightBoardItem.query().whereId(hbii).exists()).exists()

Expected behaviour

I expected the returned array to be empty, as there are no object that satisfy the relationship predicate.

Versions

  • Vuex ORM: 0.36.4
  • Vuex: 4.1.0
  • Vue: 3.2.41
@lenormf
Copy link
Author

lenormf commented Dec 3, 2022

After thinking about it, I guess I misunderstood the documentation and am trying to square a circle.

The example led me to think a relationship could be used as a predicate for the returned data, but it appears that it only controls which relationships get loaded or not — all objects will be returned in any case.

Let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant