From 7b20b11c6e1530492a02ab66c86bb5796bfb9c99 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Thu, 12 Jan 2023 16:26:33 +0100
Subject: [PATCH] add user_corpus m2m relationship
---
app/models.py | 20 ++++++++++++++++--
migrations/versions/4aa88f253dab_.py | 31 ++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 2 deletions(-)
create mode 100644 migrations/versions/4aa88f253dab_.py
diff --git a/app/models.py b/app/models.py
index d16bcfae..9b232c1a 100644
--- a/app/models.py
+++ b/app/models.py
@@ -257,7 +257,7 @@ class Avatar(HashidMixin, FileMixin, db.Model):
# Primary key
id = db.Column(db.Integer, primary_key=True)
# Foreign keys
- user_id = db.Column(db.Integer, db.ForeignKey('users.id'), unique=True)
+ user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
@property
def path(self):
@@ -276,7 +276,16 @@ class Avatar(HashidMixin, FileMixin, db.Model):
**self.file_mixin_to_json_serializeable()
}
return json_serializeable
-
+
+
+corpus_followers = db.Table(
+ 'corpus_followers',
+ db.Model.metadata,
+ db.Column('user_id', db.ForeignKey('users.id'), primary_key=True),
+ db.Column('corpus_id', db.ForeignKey('corpora.id'), primary_key=True)
+)
+
+
class User(HashidMixin, UserMixin, db.Model):
__tablename__ = 'users'
# Primary key
@@ -327,6 +336,13 @@ class User(HashidMixin, UserMixin, db.Model):
cascade='all, delete-orphan',
lazy='dynamic'
)
+ followed_corpora = db.relationship(
+ 'Corpus',
+ secondary=corpus_followers,
+ primaryjoin=(corpus_followers.c.user_id == id),
+ backref=db.backref('followers', lazy='dynamic'),
+ lazy='dynamic'
+ )
jobs = db.relationship(
'Job',
backref='user',
diff --git a/migrations/versions/4aa88f253dab_.py b/migrations/versions/4aa88f253dab_.py
new file mode 100644
index 00000000..2351532c
--- /dev/null
+++ b/migrations/versions/4aa88f253dab_.py
@@ -0,0 +1,31 @@
+"""Add corpus_followers table
+
+Revision ID: 4aa88f253dab
+Revises: 5b2a6e590166
+Create Date: 2023-01-12 14:47:39.492875
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '4aa88f253dab'
+down_revision = '5b2a6e590166'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ op.create_table(
+ 'corpus_followers',
+ sa.Column('user_id', sa.Integer(), nullable=False),
+ sa.Column('corpus_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['corpus_id'], ['corpora.id'], ),
+ sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
+ sa.PrimaryKeyConstraint('user_id', 'corpus_id')
+ )
+
+
+def downgrade():
+ op.drop_table('corpus_followers')