Browse Source

Make sure that childrens are getting removed when the parent got removed

Peter Justin 7 years ago
parent
commit
7394e255ea
4 changed files with 15 additions and 16 deletions
  1. 10 13
      flaskbb/forum/models.py
  2. 2 1
      flaskbb/management/models.py
  3. 1 1
      flaskbb/message/models.py
  4. 2 1
      flaskbb/plugins/models.py

+ 10 - 13
flaskbb/forum/models.py

@@ -148,7 +148,8 @@ class Post(HideableCRUDMixin, db.Model):
 
     id = db.Column(db.Integer, primary_key=True)
     topic_id = db.Column(db.Integer,
-                         db.ForeignKey("topics.id", ondelete="CASCADE"),
+                         db.ForeignKey("topics.id", ondelete="CASCADE",
+                                       use_alter=True),
                          nullable=True)
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=True)
     username = db.Column(db.String(200), nullable=False)
@@ -416,7 +417,8 @@ class Topic(HideableCRUDMixin, db.Model):
     # One-to-many
     posts = db.relationship("Post", backref="topic", lazy="dynamic",
                             primaryjoin="Post.topic_id == Topic.id",
-                            cascade="all, delete-orphan", post_update=True)
+                            cascade="all, delete-orphan",
+                            post_update=True)
 
     # Properties
     @property
@@ -812,11 +814,8 @@ class Forum(db.Model, CRUDMixin):
                                                 ondelete="SET NULL"),
                                   nullable=True)
 
-    last_post_user = db.relationship(
-        "User",
-        uselist=False,
-        foreign_keys=[last_post_user_id]
-    )
+    last_post_user = db.relationship("User", uselist=False,
+                                     foreign_keys=[last_post_user_id])
 
     # Not nice, but needed to improve the performance; can be set to NULL
     # if the forum has no posts
@@ -826,11 +825,8 @@ class Forum(db.Model, CRUDMixin):
                                   default=time_utcnow, nullable=True)
 
     # One-to-many
-    topics = db.relationship(
-        "Topic",
-        backref="forum",
-        lazy="dynamic"
-    )
+    topics = db.relationship("Topic", backref="forum", lazy="dynamic",
+                             cascade="all, delete-orphan")
 
     # Many-to-many
     moderators = db.relationship(
@@ -1110,7 +1106,8 @@ class Category(db.Model, CRUDMixin):
     # One-to-many
     forums = db.relationship("Forum", backref="category", lazy="dynamic",
                              primaryjoin='Forum.category_id == Category.id',
-                             order_by='asc(Forum.position)')
+                             order_by='asc(Forum.position)',
+                             cascade="all, delete-orphan")
 
     # Properties
     @property

+ 2 - 1
flaskbb/management/models.py

@@ -24,7 +24,8 @@ class SettingsGroup(db.Model, CRUDMixin):
     key = db.Column(db.String(255), primary_key=True)
     name = db.Column(db.String(255), nullable=False)
     description = db.Column(db.Text, nullable=False)
-    settings = db.relationship("Setting", lazy="dynamic", backref="group")
+    settings = db.relationship("Setting", lazy="dynamic", backref="group",
+                               cascade="all, delete-orphan")
 
     def __repr__(self):
         return "<{} {}>".format(self.__class__.__name__, self.key)

+ 1 - 1
flaskbb/message/models.py

@@ -43,7 +43,7 @@ class Conversation(db.Model, CRUDMixin):
     messages = db.relationship(
         "Message", lazy="joined", backref="conversation",
         primaryjoin="Message.conversation_id == Conversation.id",
-        order_by="asc(Message.id)"
+        order_by="asc(Message.id)", cascade="all, delete-orphan"
     )
 
     # this is actually the users message box

+ 2 - 1
flaskbb/plugins/models.py

@@ -64,7 +64,8 @@ class PluginRegistry(CRUDMixin, db.Model):
     values = db.relationship(
         'PluginStore',
         collection_class=attribute_mapped_collection('key'),
-        backref='plugin'
+        backref='plugin',
+        cascade="all, delete-orphan",
     )
 
     @property