def test_constraint_creation(self): class FKC_a(TestModel): name = CharField() fkc_deferred = DeferredRelation() class FKC_b(TestModel): fkc_a = ForeignKeyField(fkc_deferred) fkc_deferred.set_model(FKC_a) with test_db.transaction() as txn: FKC_b.drop_table(True) FKC_a.drop_table(True) FKC_a.create_table() FKC_b.create_table() # Foreign key constraint is not enforced. fb = FKC_b.create(fkc_a=-1000) fb.delete_instance() # Add constraint. test_db.create_foreign_key(FKC_b, FKC_b.fkc_a) def _trigger_exc(): with test_db.savepoint() as s1: fb = FKC_b.create(fkc_a=-1000) self.assertRaises(IntegrityError, _trigger_exc) fa = FKC_a.create(name='fa') fb = FKC_b.create(fkc_a=fa) txn.rollback()
def test_doc_example(self): db = database_initializer.get_in_memory_database() TweetDeferred = DeferredRelation() class Base(Model): class Meta: database = db class User(Base): username = CharField() favorite_tweet = ForeignKeyField(TweetDeferred, null=True) class Tweet(Base): user = ForeignKeyField(User) message = TextField() TweetDeferred.set_model(Tweet) with db.transaction(): User.create_table() Tweet.create_table() # SQLite does not support alter + add constraint. self.assertRaises( OperationalError, lambda: db.create_foreign_key(User, User.favorite_tweet))
def test_multiple_refs(self): person_ref = DeferredRelation() class Relationship(TestModel): person_from = ForeignKeyField(person_ref, related_name='f1') person_to = ForeignKeyField(person_ref, related_name='f2') class SomethingElse(TestModel): person = ForeignKeyField(person_ref) class Person(TestModel): name = CharField() person_ref.set_model(Person) p1 = Person(id=1, name='p1') p2 = Person(id=2, name='p2') p3 = Person(id=3, name='p3') r = Relationship(person_from=p1, person_to=p2) s = SomethingElse(person=p3) self.assertEqual(r.person_from.name, 'p1') self.assertEqual(r.person_to.name, 'p2') self.assertEqual(s.person.name, 'p3')
title_filter, content_filter, author_filter, metadata_filter, attribute_filter, attachment_filter ]): query += " HAVING entry.follows_id IS NULL" # sort newest first, taking into account the last edit if any # TODO: does this make sense? Should we only consider creation date? query += " ORDER BY entry.priority DESC, timestamp DESC" if n: query += " LIMIT {}".format(n) if offset: query += " OFFSET {}".format(offset) logging.debug("query=%r, variables=%r" % (query, variables)) return Entry.raw(query, *variables) DeferredEntry.set_model(Entry) class EntryChange(Model): """ Represents a change of an entry. The nomenclature here is that a *revision* is what an entry looked like at at a given point in time, while a change happens at a specific time and takes us from one revision to the next. Counter-intuitively, what's stored here is the *old* entry data. The point is that then we only need to store the fields that actually were changed! But it becomes a bit confusing when it's time to reconstruct an old entry. """
def get_json_content(self): if self.quotation is not None: return json.dumps(model_to_dict(self.quotation), default=json_serial) elif self.text_content is not None: return json.dumps(model_to_dict(self.text_content), default=json_serial) return None class Message(BaseModel): conversation = ForeignKeyField(Conversation) sender = ForeignKeyField(User) message_type = ForeignKeyField(MessageType) content = ForeignKeyField(MessageContent) display_content = TextField(null=True) ts = DateTimeField() DeferredLastMessage.set_model(Message) class ConversationParty(BaseModel): conversation = ForeignKeyField(Conversation) last_read_message = ForeignKeyField(Message, null=True) user = ForeignKeyField(User) name = CharField() picture = ForeignKeyField(Photo, null=True) class Company(BaseModel): name = CharField(null = False) description = CharField(null = False) picture = CharField(null = False) base_value = FloatField(null = False) user_datastore = PeeweeUserDatastore(database, User, Role, UserRoles)