Skip to content

Can´t create entity with two HasOne attributes pointing to same entity #322

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

Closed
milosloub opened this issue Jul 2, 2018 · 4 comments
Closed
Labels

Comments

@milosloub
Copy link
Contributor

milosloub commented Jul 2, 2018

I have two entities Client and ClientRelation. ClientRelation only stores two Client foreign keys and their relation

ClientRelation looks like this

    public class ClientRelation : Identifiable
    {
        [HasOne("client", Link.None)]
        public virtual Client Client { get; set; }
        public int ClientId { get; set; }

        [HasOne("relatedClient", Link.None)]
        public virtual Client RelatedClient { get; set; }
        public int RelatedClientId { get; set; }

        [Attr("relationship")]
        public RelationshipEnum Relationship { get; set; }
    }

After upgrade to version 2.3.2 or 2.3.3, POST on this entity throws exception:

System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Clients' when IDENTITY_INSERT is set to OFF.

Post request looks like this:

{
    "data": {
        "attributes": {
            "relationship":1
        },
        "relationships": {
        	"client": {
                "data": {
                    "type": "clients",
                    "id": "1"
                }
            },
    		"relatedClient": {
                "data": {
                    "type": "clients",
                    "id": "2"
                }
            },
        },
        "type": "client-relations"
    }
}

In version 2.3.1 everything is OK. I think that is somehow caused by new method AttachHasOnePointers() in DefaultEntityRepository. Do you know, what could be a problem?

@milosloub milosloub changed the title Can´t create entity with two HasOne attribute pointing to same entity Can´t create entity with two HasOne attributes pointing to same entity Jul 2, 2018
@jaredcnance jaredcnance added the bug label Jul 2, 2018
@jaredcnance
Copy link
Contributor

#333 opened for fix. I plan on rolling out in v2.3.5 today or tomorrow.

jaredcnance added a commit that referenced this issue Jul 5, 2018
fix(relationship pointers): key pointers by RelationshipAttribute
@milosloub
Copy link
Contributor Author

Thank you ! I'll try it when new version will be out. Fix looks great 👍

@jaredcnance
Copy link
Contributor

I am bundling it with some fixes for json:api v1.1 operations. This shouldn't impact you unless you're using bulk operations but the version has been bumped to v2.4.0-beta1

@milosloub
Copy link
Contributor Author

I tried 2.4.0-beta1 (I don't use operations ) and now everythink is ok. Thank you 👍

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

No branches or pull requests

2 participants