[Developers] State Quarters example and "need a unique result to attach here"
David Flanagan
david at davidflanagan.com
Fri Apr 4 20:39:30 UTC 2008
When I first wrote the US State Quarters example
(http://freebase.com/view/guid/9202a8c04000641f800000000544e10c#id2609142)
I specified that the type of the us_state_quarter/state property was
/type/text
The example has been modified since I wrote it. It now expects the state
property to be /location/us_state, which is good. In order to make this
change work, however, the query in the python code had to change, and
I don't understand why. Since I don't get it, I'm obviously not
explaining something right about the MQL write grammar.
Here is the original query from the example:
# This query creates a single quarter
q = {'create':'unless_exists', # Create a new object
'guid':None, # And return its guid
'type':["/common/topic", TYPEID], # Make it a topic and a
'name': fields[0] + ' State Quarter', # The object's name
'state': fields[0], # State name
'release': fields[1], # Release date
'statehood': fields[2], # Statehood date
'mintage': int(fields[3])} # How many minted
Here's the modified version as it stands now:
# This query creates a single quarter
q = {'create':'unless_exists', # Create a new object
'id':None, # And return its id
'type':["/common/topic", TYPEID], # Make it a topic and a
'name': fields[0] + ' State Quarter', # The object's name
'release': fields[1], # Release date
'statehood': fields[2], # Statehood date
'mintage': int(fields[3]), # How many minted
'state': { 'name': fields[0], 'type': '/location/us_state' }} #
The change is that the value of the state property is now an expanded
object with an explictly specified type instead of a simple state name.
I don't understand why this is necessary. I don't see why I can't
specify the state by name. It seems to me that the server knows the
expected type of the state property and could use that type and the name
I specify to uniquely identify the object I want to connect to here.
If I run my old version of the query, however (and the state quarter
object does not exist) I get the error message "Need a unique result to
attach here, not 2". From looking at the two guids it reports, it can't
decide between the state of Delaware and the city of Delaware, Iowa.
(The full error response from mqlwrite is below, if anyone wants to look
at it.) Note that once the state quarter objects exist, my version of
the query works fine: I get "create:existed" as expected.
So my questions are:
1) Is this a bug?
2) If it is not a bug then I need to better understand the matching
rules for create:unless_exists. Would this have worked if I specified
the state by id rather than by name?
3) Why isn't connect:insert or connect:update required here?
'state': { 'name': fields[0], 'type': '/location/us_state' }}
That is, why doesn't think clause create a new /location/us_state
instance? Does the create:unless_exist at the root of the query somehow
apply to this sub-query as well?
Thanks for any help!
David
This is the full error response to my write query.
{
"code": "\/api\/status\/ok",
"qname": {
"code": "\/api\/status\/error",
"messages": [
{
"code": "\/api\/status\/error\/mql\/result",
"info": {
"count": 2,
"guids": [
"#9202a8c04000641f8000000000011a70",
"#9202a8c04000641f80000000000c6866"
]
},
"message": "Need a unique result to attach here, not 2",
"path": "state",
"query": [
{
"create": "unless_exists",
"error_inside": "state",
"guid": null,
"mintage": 774824000,
"name": "Delaware State Quarter",
"release": "1999-01-04",
"state": "Delaware",
"statehood": "1787-12-07",
"type": [
"\/common\/topic",
"\/user\/docs\/default_domain\/us_state_quarter"
]
}
]
}
]
},
"status": "200 OK"
}
But if I try running the query in the old way, I get the error
More information about the Developers
mailing list