Database attempts to insert NULL values when a POST request is sent to webservice via RestKit
-
20-12-2019 - |
Question
When I make a POST request to my webservice, the following error is generated:
TinyTds::Error: Cannot insert the value NULL into column 'BankCD', table 'SlotsMasterData.Master.Bank'; column does not allow nulls.
The server console displays the following:
From the above you can see that values exist for all attributes of my Bank object, but suddenly the reference to each is NULL when 'SELECT CAST(SCOPE IDENTITY() ... ' is performed.
This is my app code:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:basePathToWebService]];
RKObjectMapping *bankMapping = [RKObjectMapping mappingForClass:[Banks class]];
NSDictionary *attributeMappingDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"BankID", @"bankID",
@"BankCD", @"bankCD",
@"BankName", @"bankName",
@"InsertSourceKey", @"insertSourceKey",
@"UpdateSourceKey", @"updateSourceKey",
@"Active", @"active",
nil];
[bankMapping addAttributeMappingsFromDictionary:attributeMappingDictionary];
RKObjectMapping *bankRequestMapping = [RKObjectMapping requestMapping];
[bankRequestMapping addAttributeMappingsFromDictionary:attributeMappingDictionary];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bankMapping method:RKRequestMethodAny pathPattern:@"/banks" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:bankRequestMapping objectClass:[Banks class] rootKeyPath:nil method:RKRequestMethodAny];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager addRequestDescriptor:requestDescriptor];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
//Post
[objectManager postObject:bank path:@"/banks" parameters:nil success:nil failure:nil];
Why is an attempt made to insert NULL values? How do I fix this? Thanks for your help!
Solution
bankRequestMapping
should be declared as the inverse mapping of the bankMapping
. As you have it at the moment the parameter names appear to be the wrong way around. You should use Charles to check what you are actually sending and that it matches what you expect.