質問編集履歴
14
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -64,45 +64,17 @@
|
|
64
64
|
|
65
65
|
--
|
66
66
|
|
67
|
-
a.py(例外処理,sqlの条件等は長くて見ずらくなるので出来るだけ除外しました)
|
68
|
-
|
69
|
-
```python
|
70
|
-
|
71
|
-
con = Con()
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
def put(self, id):
|
76
|
-
|
77
|
-
m = con.put_m(id)
|
78
|
-
|
79
|
-
# 更新有無の確認
|
80
|
-
|
81
|
-
if m is None:
|
82
|
-
|
83
|
-
print('更新対象なし')
|
84
|
-
|
85
|
-
else:
|
86
|
-
|
87
|
-
# 更新処理とインサート処理呼ぶ?
|
88
|
-
|
89
|
-
# 以下のままだと、Noneでエラーなのは認識しています。
|
90
|
-
|
91
|
-
return {'id': request.json['id']}
|
92
|
-
|
93
|
-
```
|
94
|
-
|
95
67
|
b.py
|
96
68
|
|
97
69
|
```python
|
98
70
|
|
99
71
|
class Con:
|
100
72
|
|
101
|
-
def put_m(self, id):
|
73
|
+
def put_main(self, id):
|
102
74
|
|
103
75
|
# Update
|
104
76
|
|
105
|
-
update = Con.query.filter(
|
77
|
+
update = Con.query.filter(id == id).first()
|
106
78
|
|
107
79
|
update.end = datetime.datetime.now()
|
108
80
|
|
@@ -112,47 +84,35 @@
|
|
112
84
|
|
113
85
|
else:
|
114
86
|
|
115
|
-
# updateあった
|
87
|
+
# updateaあったらinsert
|
116
88
|
|
117
|
-
|
89
|
+
data = []
|
118
90
|
|
119
91
|
# insert
|
120
92
|
|
121
93
|
insert = Con(
|
122
94
|
|
123
|
-
id =
|
95
|
+
id = data['id'],
|
124
96
|
|
125
|
-
str =
|
97
|
+
str = data['str'],
|
126
98
|
|
127
99
|
start= created_time,
|
128
100
|
|
129
101
|
end = datetime.date.max,
|
130
102
|
|
131
|
-
)
|
103
|
+
)
|
132
104
|
|
133
105
|
m_set.append(insert)
|
134
106
|
|
135
|
-
contact = Contact(**
|
107
|
+
contact = Contact(**data)
|
136
108
|
|
137
109
|
m_set.append(contact)
|
138
110
|
|
139
|
-
self.__commit
|
111
|
+
self.__commit(update, insert)
|
140
112
|
|
141
113
|
return m_set
|
142
114
|
|
143
115
|
|
144
|
-
|
145
|
-
```
|
146
|
-
|
147
|
-
せめて、セレクトだけでもと思い、下記を作成して実行したらセレクトは出来るのは確認しました。
|
148
|
-
|
149
|
-
```python
|
150
|
-
|
151
|
-
def put_m(self, id):
|
152
|
-
|
153
|
-
update = Con.query.filter(Con.id == id).first()
|
154
|
-
|
155
|
-
return update
|
156
116
|
|
157
117
|
```
|
158
118
|
|
13
文言変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
-
※
|
47
|
+
※プライマリーキーのことは、忘れてください。
|
48
48
|
|
49
49
|
|
50
50
|
|
@@ -74,7 +74,7 @@
|
|
74
74
|
|
75
75
|
def put(self, id):
|
76
76
|
|
77
|
-
m = con.put_m(
|
77
|
+
m = con.put_m(id)
|
78
78
|
|
79
79
|
# 更新有無の確認
|
80
80
|
|
12
コード削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -172,82 +172,6 @@
|
|
172
172
|
|
173
173
|
|
174
174
|
|
175
|
-
前のコード
|
176
|
-
|
177
|
-
--
|
178
|
-
|
179
|
-
b.py
|
180
|
-
|
181
|
-
```python
|
182
|
-
|
183
|
-
class Con:
|
184
|
-
|
185
|
-
def put_m(self, m_data):
|
186
|
-
|
187
|
-
m_data = Con.query.filter(Con.id == id).first()
|
188
|
-
|
189
|
-
# ここでinsertとupdateの内容を読ぶとかすればいい?
|
190
|
-
|
191
|
-
update = set(updata, insert, datetime.datetime.now())
|
192
|
-
|
193
|
-
# コミット処理
|
194
|
-
|
195
|
-
self.__commit_all(update, insert)
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
# insertとupdate文用に書いたがどう使えばいいのかわからない。
|
200
|
-
|
201
|
-
def set(self, m_data, created_time):
|
202
|
-
|
203
|
-
# update文はこれのはず
|
204
|
-
|
205
|
-
update = Con.query.filter(Con.id == id).first()
|
206
|
-
|
207
|
-
update.end = datetime.datetime.now()
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
# insert文はこんな感じなはず
|
212
|
-
|
213
|
-
insert = Con(
|
214
|
-
|
215
|
-
id = m_data['id'],
|
216
|
-
|
217
|
-
str = m_data['str'],
|
218
|
-
|
219
|
-
start= created_time,
|
220
|
-
|
221
|
-
end = datetime.date.max,
|
222
|
-
|
223
|
-
) m_set.append(insert)
|
224
|
-
|
225
|
-
contact = Contact(**m_data)
|
226
|
-
|
227
|
-
m_set.append(contact)
|
228
|
-
|
229
|
-
return m_set
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
def __commit_all(self, commit_data):
|
234
|
-
|
235
|
-
try:
|
236
|
-
|
237
|
-
db.session.add_all(commit_data)
|
238
|
-
|
239
|
-
db.session.commit()
|
240
|
-
|
241
|
-
except Exception as e:
|
242
|
-
|
243
|
-
db.session.rollback()
|
244
|
-
|
245
|
-
raise e
|
246
|
-
|
247
|
-
```
|
248
|
-
|
249
|
-
|
250
|
-
|
251
175
|
|
252
176
|
|
253
177
|
こんな感じでやれば出来るのではないかなどのアドバイスだけでも頂ければ幸いです。
|
11
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,6 +140,8 @@
|
|
140
140
|
|
141
141
|
return m_set
|
142
142
|
|
143
|
+
|
144
|
+
|
143
145
|
```
|
144
146
|
|
145
147
|
せめて、セレクトだけでもと思い、下記を作成して実行したらセレクトは出来るのは確認しました。
|
@@ -226,6 +228,22 @@
|
|
226
228
|
|
227
229
|
return m_set
|
228
230
|
|
231
|
+
|
232
|
+
|
233
|
+
def __commit_all(self, commit_data):
|
234
|
+
|
235
|
+
try:
|
236
|
+
|
237
|
+
db.session.add_all(commit_data)
|
238
|
+
|
239
|
+
db.session.commit()
|
240
|
+
|
241
|
+
except Exception as e:
|
242
|
+
|
243
|
+
db.session.rollback()
|
244
|
+
|
245
|
+
raise e
|
246
|
+
|
229
247
|
```
|
230
248
|
|
231
249
|
|
10
不要文言削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
|:--|:--:|--:|
|
40
40
|
|
41
|
-
|1|aaa|2019-08-26 23:27:13|2019-08-28 23:27:13(現在日時に更新
|
41
|
+
|1|aaa|2019-08-26 23:27:13|2019-08-28 23:27:13(現在日時に更新(now()))
|
42
42
|
|
43
43
|
|1|bbb|2019-08-26 23:27:13|9999-12-31 09:00:00(☆ここが追加(insert))
|
44
44
|
|
9
DBイメージ変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
--
|
22
22
|
|
23
|
-
(もともと
|
23
|
+
(もともとのDB(con)の状態)
|
24
24
|
|
25
25
|
|id|str|start | end
|
26
26
|
|
@@ -30,9 +30,9 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
下のように更新
|
33
|
+
上記DBが、下記のように更新されるイメージ。(↓)
|
34
|
-
|
34
|
+
|
35
|
-
teime_endを現在日時に更新して、同じidで登録(strが
|
35
|
+
teime_endを現在日時に更新して、同じidで登録する。(strが入力画面から受け取った内容)
|
36
36
|
|
37
37
|
|id|str|start | end
|
38
38
|
|
8
文言追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
"ID"が登録あった場合、入力[ID]の"end"を現在時刻に更新、更新した"ID"と入力された"str"を登録(insert)したいです。
|
14
14
|
|
15
|
-
※もはや、何をどうしたらいいのかわからなくなりました。すみません。
|
15
|
+
※もはや、何をどうしたらいいのかわからなくなりました。insertとupdateがよくわかっていないのかもしれません・・・すみません。
|
16
16
|
|
17
17
|
|
18
18
|
|
7
コード変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -108,7 +108,7 @@
|
|
108
108
|
|
109
109
|
if update is None:
|
110
110
|
|
111
|
-
r
|
111
|
+
print('NotUpdate')
|
112
112
|
|
113
113
|
else:
|
114
114
|
|
6
タイトル変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Python3】SQLAlchemyを使用し
|
1
|
+
【Python3】SQLAlchemyを使用し、UpdateとInsertを(1トランザクションで)行いたい
|
test
CHANGED
File without changes
|
5
コメント変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,13 @@
|
|
4
4
|
|
5
5
|
SQLAlchemyを使用して、UpdateとInsertを1トランザクションで行いたいです。
|
6
6
|
|
7
|
+
|
8
|
+
|
9
|
+
詳細:
|
10
|
+
|
7
|
-
|
11
|
+
入力画面上で、"ID"と"str"が入力出来る画面があります。
|
12
|
+
|
13
|
+
"ID"が登録あった場合、入力[ID]の"end"を現在時刻に更新、更新した"ID"と入力された"str"を登録(insert)したいです。
|
8
14
|
|
9
15
|
※もはや、何をどうしたらいいのかわからなくなりました。すみません。
|
10
16
|
|
4
エラー内容修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
下に、途中コードを載せますがここから先どうしたらいいのかで止まっています。
|
50
50
|
|
51
|
-
現在"
|
51
|
+
現在"name 'm_data' is not defined"エラーで止まっている状態です。
|
52
52
|
|
53
53
|
(自身で解決できれば、すぐにコードを更新致します。申し訳ありません。)
|
54
54
|
|
3
リンク・タグ追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -158,6 +158,8 @@
|
|
158
158
|
|
159
159
|
[SQLAlchemyの基本的な使い方](https://takuya-1st.hatenablog.jp/entry/2017/03/29/002701)
|
160
160
|
|
161
|
+
[SQLAlchemyで基本的なSQLクエリまとめ](https://qiita.com/ckern/items/a762b1bc0f192a55eae8)
|
162
|
+
|
161
163
|
など...
|
162
164
|
|
163
165
|
|
2
ソースコードの変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,6 +92,86 @@
|
|
92
92
|
|
93
93
|
class Con:
|
94
94
|
|
95
|
+
def put_m(self, id):
|
96
|
+
|
97
|
+
# Update
|
98
|
+
|
99
|
+
update = Con.query.filter(Con.id == id).first()
|
100
|
+
|
101
|
+
update.end = datetime.datetime.now()
|
102
|
+
|
103
|
+
if update is None:
|
104
|
+
|
105
|
+
raise NotFound('NotUpdate')
|
106
|
+
|
107
|
+
else:
|
108
|
+
|
109
|
+
# updateあったならinsert
|
110
|
+
|
111
|
+
m_data = []
|
112
|
+
|
113
|
+
# insert
|
114
|
+
|
115
|
+
insert = Con(
|
116
|
+
|
117
|
+
id = m_data['id'],
|
118
|
+
|
119
|
+
str = m_data['str'],
|
120
|
+
|
121
|
+
start= created_time,
|
122
|
+
|
123
|
+
end = datetime.date.max,
|
124
|
+
|
125
|
+
)
|
126
|
+
|
127
|
+
m_set.append(insert)
|
128
|
+
|
129
|
+
contact = Contact(**m_data)
|
130
|
+
|
131
|
+
m_set.append(contact)
|
132
|
+
|
133
|
+
self.__commit_all(update, insert)
|
134
|
+
|
135
|
+
return m_set
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
せめて、セレクトだけでもと思い、下記を作成して実行したらセレクトは出来るのは確認しました。
|
140
|
+
|
141
|
+
```python
|
142
|
+
|
143
|
+
def put_m(self, id):
|
144
|
+
|
145
|
+
update = Con.query.filter(Con.id == id).first()
|
146
|
+
|
147
|
+
return update
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
現在以下のURL参考に進めています。
|
154
|
+
|
155
|
+
--
|
156
|
+
|
157
|
+
[SQLAlchemyでUpdateするには](https://qiita.com/nskydiving/items/745f985a42da89a16934)
|
158
|
+
|
159
|
+
[SQLAlchemyの基本的な使い方](https://takuya-1st.hatenablog.jp/entry/2017/03/29/002701)
|
160
|
+
|
161
|
+
など...
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
前のコード
|
166
|
+
|
167
|
+
--
|
168
|
+
|
169
|
+
b.py
|
170
|
+
|
171
|
+
```python
|
172
|
+
|
173
|
+
class Con:
|
174
|
+
|
95
175
|
def put_m(self, m_data):
|
96
176
|
|
97
177
|
m_data = Con.query.filter(Con.id == id).first()
|
@@ -140,29 +220,7 @@
|
|
140
220
|
|
141
221
|
```
|
142
222
|
|
143
|
-
|
223
|
+
|
144
|
-
|
145
|
-
```python
|
146
|
-
|
147
|
-
def put_m(self, id):
|
148
|
-
|
149
|
-
update = Con.query.filter(Con.id == id).first()
|
150
|
-
|
151
|
-
return update
|
152
|
-
|
153
|
-
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
現在以下のURL参考に進めています。
|
158
|
-
|
159
|
-
--
|
160
|
-
|
161
|
-
[SQLAlchemyでUpdateするには](https://qiita.com/nskydiving/items/745f985a42da89a16934)
|
162
|
-
|
163
|
-
[SQLAlchemyの基本的な使い方](https://takuya-1st.hatenablog.jp/entry/2017/03/29/002701)
|
164
|
-
|
165
|
-
など...
|
166
224
|
|
167
225
|
|
168
226
|
|
1
タグの追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|