Skip to content

Commit 9222102

Browse files
committed
Fixed a get_multi bug with strings
1 parent eac6215 commit 9222102

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

backend/PyMatcha/utils/orm/_model.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
from copy import deepcopy
3+
from typing import List
34

45
import pymysql
56
from PyMatcha import database_config
@@ -254,7 +255,7 @@ def get(cls, **kwargs):
254255
raise ValueError("Not found.")
255256

256257
@classmethod
257-
def get_multi(cls, **kwargs):
258+
def get_multi(cls, **kwargs) -> List:
258259
"""
259260
Get a model from the database, using multiple keyword argument as a filter.
260261
@@ -309,7 +310,6 @@ def get_multis(cls, **kwargs):
309310
model = Model.get(username="test", email="test@example.org")
310311
311312
Returns list of instances on success and raises an error if the row count was 0
312-
313313
"""
314314

315315
keys = []
@@ -321,11 +321,16 @@ def get_multis(cls, **kwargs):
321321
where = ""
322322
length = len(keys)
323323
for index, (key, value) in enumerate(zip(keys, values)):
324-
if index == length - 1:
325-
where = where + f"{key}={value}"
324+
if isinstance(value, str):
325+
if index == length - 1:
326+
where = where + f"{key}='{value}'"
327+
else:
328+
where = where + f"{key}='{value}' and "
326329
else:
327-
where = where + f"{key}={value} and "
328-
330+
if index == length - 1:
331+
where = where + f"{key}={value}"
332+
else:
333+
where = where + f"{key}={value} and "
329334
temp = cls()
330335
with temp.db.cursor() as c:
331336
c.execute(

backend/populate_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def gen_datetime(min_year: int = 1900, max_year: int = datetime.datetime.now().y
2323
def populate_users():
2424
User.drop_table()
2525
User.create_table()
26-
for i in range(0, 500):
26+
for i in range(0, 150):
2727
gender = random.choice(["male", "female", "other"])
2828

2929
orientation = random.choice(["heterosexual", "homosexual", "bisexual", "other"])
@@ -80,7 +80,7 @@ def populate_users():
8080
with open("tags.json") as handle:
8181
json_list = json.load(handle)
8282
u = get_user(username)
83-
tags = random.sample(json_list, 5)
83+
tags = list(set(random.sample(json_list, 8)))
8484

8585
for tag in tags:
8686
Tag.create(name=tag, user_id=u.id)

0 commit comments

Comments
 (0)