Ask a questionAsk a question
 

AnswerNullable Foreign Keys

  • Monday, October 26, 2009 3:47 PMEllen_B Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi there,

    We've got the following (for explanatory reasons simplified) situation. There is an Order table and an Items table and between those there's a foreign key relationship defined. The foreign key column on the Item table does allow null values so Items may exist without an Order. Creating this relationship:

    Order 0..1 --<> * Item

    For example, there are 2 Items in the database, one belonging to an Order and another one standing on its own (i.e. has a null value in the foreign key column). Now when i do a request for the one belonging to an Order like this:

    http://localhost:4717/WebDataService1.svc/Items(1)/Orders

    I get the parent Order as result back from the service which is logical as there is a valid foreign key relationship. Now when i do a request for the unrelated Item like this:

    http://localhost:4717/WebDataService1.svc/Items(2)/Orders

    I won't get a response at all. The server returns just nothing, like it is hanging. Shouldn't the server at least return something? Like when you query for Items for an Order which has none you at least get an empty result back which you can handle in your application...just hanging there seems so useless ;-)

    I hope i'm clear enough

    Note: This a readonly DataService

    Thanks,

    E

    • Edited byEllen_B Monday, October 26, 2009 3:50 PMadded note
    •  

Answers

  • Monday, October 26, 2009 4:10 PMPratik Patel - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    When a reference navigation property is null, the server returns status code (204) - which means No Content. And the payload is empty. Since this is a reference property, there is nothing else we can return in the payload. For collection navigation properties, we do return a empty collection.

    Hope this helps.

    Thanks
    Pratik
    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Monday, October 26, 2009 4:10 PMPratik Patel - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    When a reference navigation property is null, the server returns status code (204) - which means No Content. And the payload is empty. Since this is a reference property, there is nothing else we can return in the payload. For collection navigation properties, we do return a empty collection.

    Hope this helps.

    Thanks
    Pratik
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, November 09, 2009 12:25 PMEllen_B Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Understood,

    Thanks Pratik, for clearing it up

    Greetings,

    E