locked
Trouble with saving data with 1 to 0..1 relation RRS feed

  • Question

  • I have following simple data model: a parent entity which has 2 relations to 2 child entities called ChildA en ChildB. The type of relation is 1 to 0..1.

    I have a (saved) record which has only a childA relation and execute following code (with button when record is shown):

        screen.Parent.ChildA.deleteEntity();
        screen.Parent.setChildB(new myapp.ChildB);
        screen.Parent.ChildB.BProp1 = "test";
        screen.Parent.ChildB.BProp2 = "test";
        screen.Parent.ChildB.BProp3 = "test";
        screen.Parent.ChildB.deleteEntity();
        screen.Parent.setChildA(new myapp.ChildA);
        screen.Parent.ChildA.AProp1 = "final";
        screen.Parent.ChildA.AProp2 = "final";

    When I hit save afterwards, I get:

    What am I doing wrong?

    This is what's going over the wire:

    --batch_1c90-fa5c-f201
    Content-Type: multipart/mixed; boundary=changeset_0c72-4e38-ea49
    
    --changeset_0c72-4e38-ea49
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    POST ChildAs HTTP/1.1
    Content-ID: 1
    DataServiceVersion: 3.0
    Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
    Content-Type: application/json;odata=verbose
    MaxDataServiceVersion: 3.0
    
    {"AProp1":"final","AProp2":"final"}
    --changeset_0c72-4e38-ea49
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    MERGE Parents(13) HTTP/1.1
    Content-ID: 0
    DataServiceVersion: 3.0
    If-Match: W/"X'000000000000273C'"
    Prefer: return-content
    Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
    Content-Type: application/json;odata=verbose
    MaxDataServiceVersion: 3.0
    
    {"ChildA":{"__metadata":{"uri":"$1"}}}
    --changeset_0c72-4e38-ea49
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    DELETE ChildAs(13) HTTP/1.1
    Content-ID: 2
    DataServiceVersion: 3.0
    If-Match: W/"X'000000000000273D'"
    Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
    MaxDataServiceVersion: 3.0
    
    
    --changeset_0c72-4e38-ea49
    Content-Type: application/http
    Content-Transfer-Encoding: binary
    
    DELETE Parents(13)/$links/ChildB HTTP/1.1
    Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
    MaxDataServiceVersion: 3.0
    
    
    --changeset_0c72-4e38-ea49--
    
    --batch_1c90-fa5c-f201--


    paul van bladel ==independent enterprise application architect== http://blog.pragmaswitch.com

    Thursday, March 5, 2015 3:00 PM

All replies

  • Hi Paul,

    It is unusual for you to be posting a question you normally answer them :)

    I decided to run some tests based on your example out of curiosity.

    I got to test 'C' here is a workaround but crucially also not a single transaction.

    myapp.AddEditParent1.TestEditC_execute = function (screen) {
        var parent = screen.Parent,
            child = parent.ChildA;
        child.deleteEntity();
    
        screen.details.dataWorkspace.ApplicationData.saveChanges()
            .then(function (res) {
                var childA = new myapp.ChildA();
                childA.AProp1 = "final-C";
                childA.AProp2 = "final-C";
                childA.AProp3 = "final-C";
                parent.setChildA(childA);
            });
    };

    That got me thinking that actually the result of you code is an update in the end really ;)

    myapp.AddEditParent1.TestEditD_execute = function (screen) {
        screen.Parent.ChildA.AProp1 = "final-D";
        screen.Parent.ChildA.AProp2 = "final-D";
        screen.Parent.ChildA.AProp3 = "final-D";
    };

    So that was test D which obviously worked!

    I think it is a bug.

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, March 5, 2015 7:45 PM
  • Dave, in your test you add saveChanges() and it works? Isn't that all Paul need to add to fix his problem?


    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com

    Thursday, March 5, 2015 8:28 PM
  • Thanks dave for doing the effort the reproduce the problem. 

    Indeed probably a bug.

    @michael. Sure, it could be a workaround when things would be all in the code but i provided the sample for simulating user actions.


    paul van bladel ==independent enterprise application architect== http://blog.pragmaswitch.com

    Thursday, March 5, 2015 8:52 PM
  • I guess it would be possible to check dataworkspace changes for a matching deleted ChildA then undelete and update instead of creating a new ChildA.

    That is all the ideas I can come up with, it may be worth coding if the case is simple enough and the need great enough.

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, March 5, 2015 9:29 PM
  • thanks Dave.

    Hopefully, someone from the product team can look into this problem.

    Cheers

    paul.


    paul van bladel ==independent enterprise application architect== http://blog.pragmaswitch.com

    Friday, March 6, 2015 8:32 AM