         // assert that relationship is to-many
         assert(relationship.toMany, "Relationship \(key) on \(self.entity.name) is not to-many")
         
+        let value: AnyObject? = self.valueForKey(key)
+        
+        if value == nil {
+            
+            return nil
+        }
+        
         if relationship.ordered {
             
-            let orderedSet = self.valueForKey(key) as NSOrderedSet
+            let orderedSet = value as NSOrderedSet
             
             return orderedSet.array as [NSManagedObject]
         }
         
-        let set = self.valueForKey(key) as NSSet
+        let set = value as NSSet
         
         return set.allObjects  as [NSManagedObject]
     }

internal extension NSManagedObject {
    
    /** Get an array from a to-many relationship. */
    func arrayValueForToManyRelationship(toManyRelationship key: String) -> [NSManagedObject] {
        
        // assert existence of relationship
        assert(self.entity.relationshipsByName[key] as? NSRelationshipDescription != nil, "Relationship \(key) doesnt exist on \(self.entity.name)")

            if !relationshipDescription.toMany {
                
                // get destination resource
                if let destinationResource = managedObject.valueForKey(relationshipName) as? NSManagedObject {
                    
                    // get resource ID
                    let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
                    
                    // add to JSON object
                    jsonObject[relationshipName] = destinationResourceID
                }
            }
            
            // to-many relationship
            else {
                
                if let arrayValue = managedObject.arrayValueForToManyRelationship(toManyRelationship: relationshipName) {
                    
                    // get resource IDs
                    var resourceIDs = [UInt]()
                    
                    for destinationResource in arrayValue {
                        
                        let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
                        
                        resourceIDs.append(destinationResourceID)
                    }
                    
                    // add to jsonObject
                    jsonObject[relationshipName] = resourceIDs
                }
            }
        }
        
