graphql-python / graphql-python/graphene-sqlalchemy

Invalid id format when retrieving node

オープン
#88 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
985
フォーク
223
PR マージ指標
30日以内にマージされた PR はありません

説明

Im having issues retrieving nodes by id that have composed primary keys.

I was getting an error:

> File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 846, in _get_impl
> ','.join("'%s'" % c for c in mapper.primary_key))
> InvalidRequestError: Incorrect number of values in identifier to formulate primary key for query.get(); primary key columns are xxxxxxxx
>

So I override **_get_node_** method to see what I was receiving as id and this it:
```
ID TYPE:
ID: (1L, '1')
```

So I had to fix it by turning that unicode id into a list, trying to re-build the primary key of the schema's model, using sqlalchemy mappers:
```python
@classmethod
def get_node(cls, info, id):
id_parts = id.strip('()').split(',')
parts = []
pk = cls._meta.model.__mapper__.primary_key
for n, k in enumerate(pk):
part = id_parts[n].strip()
p_type = k.type.python_type
if p_type in (int, long, float):
if unicode.isalpha(part[-1]):
part = part[:-1]
parts.append(p_type(part))

else:
# remove inner string/unicode repr
stripped_part = re.sub(r"[u]?'(.*?)'", "\\1", part)
parts.append(stripped_part)

return cls.get_query(info).get(parts)
```

@syrusakbary I'm wondering why I'm getting a unicode when the ```SQLAlchemyObjectType.resolve_id``` returns a tuple

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。