def save(self): """Writes the instance to the database. If the instance is created, a new record will be added to the database else if it is loaded from database, the record will be updated. It also saves all dirty instances of related model instances referenced by :class:`ManyToOne` properties. :returns: an unique key id :raises: :class:`DatabaseError` if instance could not be commited. """ if self.is_saved and not self.is_dirty: return self.key from kalapy.db.engines import database objects = self._get_related() + [self] # first save all related records database.update_records(*objects) return self.key
def save(self): """Writes the instance to the database. If the instance is created, a new record will be added to the database else if it is loaded from database, the record will be updated. It also saves all dirty instances of related model instances referenced by :class:`ManyToOne` properties. :returns: an unique key id :raises: :class:`DatabaseError` if instance could not be commited. """ if self.is_saved and not self.is_dirty: return self.key from kalapy.db.engines import database objects = self._get_related() + [self] # first save all related records database.update_records(*objects) return self.key
def test_update_records(self): a1 = Article(title='title') a1.save() a1.title = 'something' a2 = Article(title='title2') a3 = Article(title='title3') keys = database.update_records(a1, a2, a3) self.assertEqual(keys, [a1.key, a2.key, a3.key]) a = Article.get(a1.key) self.assertTrue(a.title != 'title')