Skip to content

Commit

Permalink
Merge pull request #362 from MasoniteFramework/fix/358
Browse files Browse the repository at this point in the history
Fix/358
  • Loading branch information
josephmancuso authored Feb 3, 2021
2 parents b3249f6 + 1e40a79 commit c65b272
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/masoniteorm/collection/Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def merge(self, items):
self._items += items
return self

def pluck(self, value, key=None):
def pluck(self, value, key=None, keep_nulls=True):
if key:
attributes = {}
else:
Expand All @@ -245,6 +245,9 @@ def pluck(self, value, key=None):
iterable = self.all().items()

for k, v in iterable:
if keep_nulls is False and v is None:
continue

if k == value:
if key:
attributes[self._data_get(item, key)] = self._data_get(
Expand Down
1 change: 1 addition & 0 deletions src/masoniteorm/relationships/BaseRelationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, fn, local_key=None, foreign_key=None):
self.foreign_key = local_key
else:
self.fn = fn
self.local_key = local_key
self.foreign_key = foreign_key

def __set_name__(self, cls, name):
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/BelongsTo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_related(self, query, relation, eagers=()):
if isinstance(relation, Collection):
return builder.where_in(
f"{builder.get_table_name()}.{self.foreign_key}",
relation.pluck(self.local_key).unique(),
relation.pluck(self.local_key, keep_nulls=False).unique(),
).get()
else:
return builder.where(
Expand Down
3 changes: 2 additions & 1 deletion src/masoniteorm/relationships/BelongsToMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def get_related(self, query, relation, eagers=None):

if isinstance(relation, Collection):
final_result = result.where_in(
self.local_owner_key, relation.pluck(self.local_owner_key)
self.local_owner_key,
relation.pluck(self.local_owner_key, keep_nulls=False),
).get()
else:
final_result = result.where(
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/relationships/HasMany.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_related(self, query, relation, eagers=None):
if isinstance(relation, Collection):
return builder.where_in(
f"{builder.get_table_name()}.{self.foreign_key}",
relation.pluck(self.local_key).unique(),
relation.pluck(self.local_key, keep_nulls=False).unique(),
).get()
else:
return builder.where(
Expand Down
7 changes: 4 additions & 3 deletions src/masoniteorm/relationships/HasOne.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class HasOne(BaseRelationship):

def __init__(self, fn, foreign_key=None, local_key=None):
if isinstance(fn, str):
self.local_key = fn
self.foreign_key = local_key
self.foreign_key = fn
self.local_key = foreign_key or "id"
else:
self.fn = fn
self.local_key = local_key or "id"
self.foreign_key = foreign_key

def apply_query(self, foreign, owner):
Expand Down Expand Up @@ -44,7 +45,7 @@ def get_related(self, query, relation, eagers=()):
if isinstance(relation, Collection):
return builder.where_in(
f"{builder.get_table_name()}.{self.foreign_key}",
relation.pluck(self.local_key).unique(),
relation.pluck(self.local_key, keep_nulls=False).unique(),
).get()
else:
return builder.where(
Expand Down
4 changes: 1 addition & 3 deletions tests/mssql/grammar/test_mssql_update_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def test_can_compile_update_with_multiple_where(self):
self.assertEqual(to_sql, sql)

def test_raw_expression(self):
to_sql = self.builder.update(
{"name": Raw("[username]")}, dry=True
).to_sql()
to_sql = self.builder.update({"name": Raw("[username]")}, dry=True).to_sql()

sql = "UPDATE [users] SET [users].[name] = [username]"
self.assertEqual(to_sql, sql)
4 changes: 1 addition & 3 deletions tests/mysql/grammar/test_mysql_update_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def test_can_compile_decrement(self):
self.assertEqual(to_sql, sql)

def test_raw_expression(self):
to_sql = self.builder.update(
{"name": Raw("`username`")}, dry=True
).to_sql()
to_sql = self.builder.update({"name": Raw("`username`")}, dry=True).to_sql()

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
Expand Down
4 changes: 1 addition & 3 deletions tests/postgres/grammar/test_update_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def test_can_compile_decrement(self):
self.assertEqual(to_sql, sql)

def test_raw_expression(self):
to_sql = self.builder.update(
{"name": Raw('"username"')}, dry=True
).to_sql()
to_sql = self.builder.update({"name": Raw('"username"')}, dry=True).to_sql()

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
Expand Down
4 changes: 1 addition & 3 deletions tests/sqlite/grammar/test_sqlite_update_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def test_can_compile_decrement(self):
self.assertEqual(to_sql, sql)

def test_raw_expression(self):
to_sql = self.builder.update(
{"name": Raw('"username"')}, dry=True
).to_sql()
to_sql = self.builder.update({"name": Raw('"username"')}, dry=True).to_sql()

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
Expand Down

0 comments on commit c65b272

Please sign in to comment.