質問するログイン新規登録

回答編集履歴

1

fixed typo

2018/03/27 02:13

投稿

tell_k
tell_k

スコア2120

answer CHANGED
@@ -1,25 +1,25 @@
1
1
  `with_entries` で relationship(=user)を引数で渡して見ました。
2
2
 
3
- ```
3
+ ```python
4
4
  session.query(Photo).with_entities(Photo.user).all()
5
5
  ```
6
6
 
7
7
  この結果生成されるSQLは下記のようになりました。
8
8
 
9
- ```
9
+ ```sql
10
10
  SELECT users.id = photos.user_id AS user FROM users, photos
11
11
  ```
12
12
 
13
13
  `with_entries` を使うと、取得される user には bool値がセットされるようです。
14
14
 
15
- ```
15
+ ```python
16
16
  for p in session.query(Photo).with_entities(Photo.user).all():
17
17
  print(p.user) # => True
18
18
  ```
19
19
 
20
20
  回避策としては `with_entries` を外してクエリを投げるのが一番無難かと思います。
21
21
 
22
- ```
22
+ ```python
23
23
  for p in session.query(Photo).all():
24
24
  print(p.user.username) # => "user name"
25
25
  ```