Skip to content

Commit 436cdf5

Browse files
authored
Merge pull request #51 from octabytes/feature
issue #45 fixed
2 parents 2ffc624 + 8c0f19a commit 436cdf5

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Versions should comply with PEP440. For a discussion on single-sourcing
1717
# the version across setup.py and the project code, see
1818
# https://packaging.python.org/en/latest/single_source_version.html
19-
version='1.2.5',
19+
version='1.2.6',
2020

2121
description="FireO ORM is specifically designed for the Google's Firestore",
2222
long_description=long_description,

src/fireo/queries/query_wrapper.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fireo.fields import ReferenceField, NestedModel
1+
from fireo.fields import ReferenceField, NestedModel, IDField
22
from fireo.queries import errors
33
from fireo.utils import utils
44
from google.cloud import firestore
@@ -50,6 +50,12 @@ def from_query_result(cls, model, doc, nested_doc=False):
5050

5151
# If it is not nested model then set the id for this model
5252
if not nested_doc:
53+
# When getting document attach the IDField if there is no user specify
54+
# it will prevent to generate new id everytime when document save
55+
# For more information see issue #45 https://github.com/octabytes/FireO/issues/45
56+
if model._meta.id is None:
57+
model._meta.id = ('id', IDField())
58+
5359
setattr(model, '_id', doc.id)
5460
# save the firestore reference doc so that further actions can be performed (i.e. collections())
5561
model._meta.set_reference_doc(doc.reference)

src/tests/v010/simple_id_key_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ def test_without_id_field():
7979

8080

8181
def test_without_id_field_but_giving_id():
82-
e1 = Employee()
82+
class EmployeeIDField(mdl.Model):
83+
name = mdl.TextField()
84+
85+
e1 = EmployeeIDField()
8386
e1.id = 'test_without_id_but_giving_id'
8487
e1.name = 'test_without_id_but_give_some_id'
8588
e1.save()
8689

8790
assert e1.id != 'test_without_id_but_giving_id'
8891

89-
e2 = Employee.collection.get(e1.key)
92+
e2 = EmployeeIDField.collection.get(e1.key)
9093
assert e2.id != 'test_without_id_but_giving_id'
9194
assert e1.name == e2.name
9295
assert e1.key == e2.key

src/tests/v12/test_issue_45.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from fireo.fields import TextField, NumberField
2+
from fireo.models import Model
3+
4+
5+
def test_fix_issue_45():
6+
class Student(Model):
7+
name = TextField()
8+
age = NumberField()
9+
10+
s = Student()
11+
s.name = 'abc'
12+
s.age = 7
13+
s.save()
14+
15+
student = Student.collection.get(s.key)
16+
17+
assert student.key == s.key
18+
19+
student.age = 10
20+
student.save()
21+
22+
assert student.key == s.key
23+
24+
student.save()
25+
26+
assert student.key == s.key
27+
28+
student.save()
29+
30+
assert student.key == s.key

0 commit comments

Comments
 (0)