Skip to content

Commit

Permalink
fixed models not loading their primary keys when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Oct 1, 2020
1 parent 724110f commit bbba3f5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
"""
self.grammar = grammar
self._table = table
self.dry = dry
self.connection = connection
self._connection = None
self._connection_details = connection_details
Expand Down Expand Up @@ -786,9 +787,14 @@ def update(self, updates: dict, dry=False):
Returns:
self
"""
if self._model and self._model.is_loaded():
self.where(
self._model.get_primary_key(), self._model.get_primary_key_value()
)

self._updates += (UpdateQueryExpression(updates),)
self.set_action("update")
if dry:
if dry or self.dry:
return self

return self.new_connection().query(self.to_sql(), self._bindings)
Expand Down Expand Up @@ -960,7 +966,7 @@ def _register_relationships_to_model(

hydrated_model.add_relation({relation_key: related_result or {}})
return self

def find(self, record_id):
"""Finds a row by the primary key ID. Requires a model
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/builder/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_builder(self, table="users"):
grammar=self.grammar,
connection=connection,
table=table,
model=User,
model=User(),
connection_details=DATABASES,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/postgres/builder/test_postgres_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_default_query_grammar(cls):
class BaseTestQueryBuilder:
def get_builder(self, table="users"):
connection = MockConnectionFactory().make("default")
return QueryBuilder(self.grammar, connection, table=table, model=Model)
return QueryBuilder(self.grammar, connection, table=table, model=Model())

def test_sum(self):
builder = self.get_builder()
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlite/builder/test_sqlite_builder_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_builder(self, table="users"):
connection=connection,
table=table,
# model=User,
connection_details={},
connection_details=DATABASES,
).on("sqlite")

def test_insert(self):
Expand Down
35 changes: 35 additions & 0 deletions tests/sqlite/models/test_sqlite_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import inspect
import unittest

from src.masoniteorm.query import QueryBuilder
from src.masoniteorm.query.grammars import SQLiteGrammar
from src.masoniteorm.connections import ConnectionFactory
from src.masoniteorm.relationships import belongs_to
from src.masoniteorm.models import Model
from tests.utils import MockConnectionFactory
from config.database import DATABASES


class User(Model):
__connection__ = "sqlite"
__timestamps__ = False
__dry__ = True


class BaseTestQueryRelationships(unittest.TestCase):

maxDiff = None

def test_update_specific_record(self):
user = User.first()
sql = user.update({"name": "joe"}).to_sql()

self.assertEqual(
sql,
"""UPDATE "users" SET "name" = 'joe' WHERE "id" = '{}'""".format(user.id),
)

def test_update_all_records(self):
sql = User.update({"name": "joe"}).to_sql()

self.assertEqual(sql, """UPDATE "users" SET "name" = 'joe'""")

0 comments on commit bbba3f5

Please sign in to comment.