A alembic/versions/ebbe2ad47fef_add_taggings_pkey.py => alembic/versions/ebbe2ad47fef_add_taggings_pkey.py +22 -0
@@ 0,0 1,22 @@
+"""add indexes
+
+Revision ID: ebbe2ad47fef
+Revises: bf4bf948f860
+Create Date: 2018-09-22 13:55:16.188388
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = 'ebbe2ad47fef'
+down_revision = 'bf4bf948f860'
+
+from alembic import op
+
+
+def upgrade():
+ # taggings must be free from duplicates and must not have a primary key
+ op.create_primary_key('taggings_pkey', 'taggings', ['song', 'tag'])
+
+
+def downgrade():
+ op.drop_constraint('PRIMARY', 'taggings_pkey', type_='primary')
M stewdio/types/tag.py => stewdio/types/tag.py +1 -1
@@ 6,7 6,7 @@ from ..database import Base
class Tag(Base):
__tablename__ = "tags"
id = sa.Column(sa.Integer, primary_key=True)
- name = sa.Column(sa.Text, nullable=False, unique=True)
+ name = sa.Column(sa.Text, nullable=False, unique=True, index=True)
songs = sa.orm.relationship("Song",
secondary="taggings",
collection_class=set,
M stewdio/types/tagging.py => stewdio/types/tagging.py +2 -2
@@ 3,6 3,6 @@ import sqlalchemy as sa
from stewdio.database import Base
taggings = sa.Table("taggings", Base.metadata,
- sa.Column("song", sa.Integer, sa.ForeignKey("songs.id"), nullable=False, index=True),
- sa.Column("tag", sa.Integer, sa.ForeignKey("tags.id"), nullable=False, index=True)
+ sa.Column("song", sa.Integer, sa.ForeignKey("songs.id"), nullable=False, index=True, primary_key=True),
+ sa.Column("tag", sa.Integer, sa.ForeignKey("tags.id"), nullable=False, index=True, primary_key=True)
)