diff --git a/README.md b/README.md index c6b751d..015faa3 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,19 @@ to organize the files of the project according to what they do. This means that templates are grouped together in one directory, static in another and views in a third (copy/paste from Flask documentation). +### DB relationships: + + **Author -> Book** should be a many-to-many relationship as multiple authors can +share the ownership of a book. + +For example, authors A and B wrote the book X. When listing all the books +written by author A, book X must appear in the results. Likewise when +listing the books written by author B. + +If we want to use a one-to-one relationship, the book X should only be +returned as part of the results if we search by "the books written by author +A **and** author B" working together. + ## Usage ``` flask --app app --debug run diff --git a/app/models.py b/app/models.py index 52925a6..a45da92 100644 --- a/app/models.py +++ b/app/models.py @@ -17,3 +17,24 @@ class Book(db.Model): shared = db.Column(db.Boolean, default=False) rating = db.Column(db.Integer) id_user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + id_author = db.Column(db.Integer, db.ForeignKey('author.id'), nullable=False) + id_editorial = db.Column(db.Integer, db.ForeignKey('editorial.id'), nullable=False) + +class Author(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255), nullable=False) + country = db.Column(db.String(255)) + +class Editorial(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255), nullable=False) + +# many-to-many books/tags +tags = db.Table('books_tags', + db.Column('id_tag', db.Integer, db.ForeignKey('tag.id'), primary_key=True), + db.Column('id_book', db.Integer, db.ForeignKey('book.id'), primary_key=True) + ) + +class Tag(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255), primary_key=True) diff --git a/migrations/versions/11ddd75a4034_.py b/migrations/versions/11ddd75a4034_.py new file mode 100644 index 0000000..232897f --- /dev/null +++ b/migrations/versions/11ddd75a4034_.py @@ -0,0 +1,36 @@ +"""empty message + +Revision ID: 11ddd75a4034 +Revises: a56eb0f88d08 +Create Date: 2022-08-07 22:47:25.294115 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '11ddd75a4034' +down_revision = 'a56eb0f88d08' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('editorial', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.add_column('book', sa.Column('id_editorial', sa.Integer(), nullable=False)) + op.create_foreign_key(None, 'book', 'editorial', ['id_editorial'], ['id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'book', type_='foreignkey') + op.drop_column('book', 'id_editorial') + op.drop_table('editorial') + # ### end Alembic commands ### diff --git a/migrations/versions/2f3143ca16e9_.py b/migrations/versions/2f3143ca16e9_.py new file mode 100644 index 0000000..cc44ddc --- /dev/null +++ b/migrations/versions/2f3143ca16e9_.py @@ -0,0 +1,33 @@ +"""empty message + +Revision ID: 2f3143ca16e9 +Revises: 70f7fdfafc25 +Create Date: 2022-08-07 18:04:12.614018 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2f3143ca16e9' +down_revision = '70f7fdfafc25' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('author', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('country', sa.String(length=255), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('author') + # ### end Alembic commands ### diff --git a/migrations/versions/5c34faeea863_.py b/migrations/versions/5c34faeea863_.py new file mode 100644 index 0000000..64b6a0e --- /dev/null +++ b/migrations/versions/5c34faeea863_.py @@ -0,0 +1,39 @@ +"""empty message + +Revision ID: 5c34faeea863 +Revises: 11ddd75a4034 +Create Date: 2022-08-07 23:25:27.237821 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '5c34faeea863' +down_revision = '11ddd75a4034' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('tag', + sa.Column('id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('tags', + sa.Column('id_tag', sa.Integer(), nullable=False), + sa.Column('id_book', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['id_book'], ['book.id'], ), + sa.ForeignKeyConstraint(['id_tag'], ['tag.id'], ), + sa.PrimaryKeyConstraint('id_tag', 'id_book') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('tags') + op.drop_table('tag') + # ### end Alembic commands ### diff --git a/migrations/versions/68152efb549c_.py b/migrations/versions/68152efb549c_.py new file mode 100644 index 0000000..bfda19b --- /dev/null +++ b/migrations/versions/68152efb549c_.py @@ -0,0 +1,44 @@ +"""empty message + +Revision ID: 68152efb549c +Revises: 5c34faeea863 +Create Date: 2022-08-07 23:26:05.032691 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = '68152efb549c' +down_revision = '5c34faeea863' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('books_tags', + sa.Column('id_tag', sa.Integer(), nullable=False), + sa.Column('id_book', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['id_book'], ['book.id'], ), + sa.ForeignKeyConstraint(['id_tag'], ['tag.id'], ), + sa.PrimaryKeyConstraint('id_tag', 'id_book') + ) + op.drop_table('tags') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('tags', + sa.Column('id_tag', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), + sa.Column('id_book', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint(['id_book'], ['book.id'], name='tags_ibfk_1'), + sa.ForeignKeyConstraint(['id_tag'], ['tag.id'], name='tags_ibfk_2'), + sa.PrimaryKeyConstraint('id_tag', 'id_book'), + mysql_default_charset='latin1', + mysql_engine='InnoDB' + ) + op.drop_table('books_tags') + # ### end Alembic commands ### diff --git a/migrations/versions/__pycache__/11ddd75a4034_.cpython-39.pyc b/migrations/versions/__pycache__/11ddd75a4034_.cpython-39.pyc new file mode 100644 index 0000000..b844bfb Binary files /dev/null and b/migrations/versions/__pycache__/11ddd75a4034_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/2f3143ca16e9_.cpython-39.pyc b/migrations/versions/__pycache__/2f3143ca16e9_.cpython-39.pyc new file mode 100644 index 0000000..04d0268 Binary files /dev/null and b/migrations/versions/__pycache__/2f3143ca16e9_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/5c34faeea863_.cpython-39.pyc b/migrations/versions/__pycache__/5c34faeea863_.cpython-39.pyc new file mode 100644 index 0000000..3009af1 Binary files /dev/null and b/migrations/versions/__pycache__/5c34faeea863_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/68152efb549c_.cpython-39.pyc b/migrations/versions/__pycache__/68152efb549c_.cpython-39.pyc new file mode 100644 index 0000000..4f51f3c Binary files /dev/null and b/migrations/versions/__pycache__/68152efb549c_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/70f7fdfafc25_.cpython-39.pyc b/migrations/versions/__pycache__/70f7fdfafc25_.cpython-39.pyc index 2c19604..f625a8f 100644 Binary files a/migrations/versions/__pycache__/70f7fdfafc25_.cpython-39.pyc and b/migrations/versions/__pycache__/70f7fdfafc25_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/a56eb0f88d08_.cpython-39.pyc b/migrations/versions/__pycache__/a56eb0f88d08_.cpython-39.pyc new file mode 100644 index 0000000..1daa747 Binary files /dev/null and b/migrations/versions/__pycache__/a56eb0f88d08_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/ab8d661b4728_.cpython-39.pyc b/migrations/versions/__pycache__/ab8d661b4728_.cpython-39.pyc new file mode 100644 index 0000000..e6623ff Binary files /dev/null and b/migrations/versions/__pycache__/ab8d661b4728_.cpython-39.pyc differ diff --git a/migrations/versions/__pycache__/b9d22c840958_.cpython-39.pyc b/migrations/versions/__pycache__/b9d22c840958_.cpython-39.pyc new file mode 100644 index 0000000..9bdf186 Binary files /dev/null and b/migrations/versions/__pycache__/b9d22c840958_.cpython-39.pyc differ diff --git a/migrations/versions/a56eb0f88d08_.py b/migrations/versions/a56eb0f88d08_.py new file mode 100644 index 0000000..bf98032 --- /dev/null +++ b/migrations/versions/a56eb0f88d08_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: a56eb0f88d08 +Revises: ab8d661b4728 +Create Date: 2022-08-07 22:43:43.906499 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a56eb0f88d08' +down_revision = 'ab8d661b4728' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('author', sa.Column('name', sa.String(length=255), nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('author', 'name') + # ### end Alembic commands ### diff --git a/migrations/versions/ab8d661b4728_.py b/migrations/versions/ab8d661b4728_.py new file mode 100644 index 0000000..c25f77c --- /dev/null +++ b/migrations/versions/ab8d661b4728_.py @@ -0,0 +1,32 @@ +"""empty message + +Revision ID: ab8d661b4728 +Revises: 2f3143ca16e9 +Create Date: 2022-08-07 18:07:37.773083 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = 'ab8d661b4728' +down_revision = '2f3143ca16e9' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('author', 'name') + op.add_column('book', sa.Column('id_author', sa.Integer(), nullable=False)) + op.create_foreign_key(None, 'book', 'author', ['id_author'], ['id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'book', type_='foreignkey') + op.drop_column('book', 'id_author') + op.add_column('author', sa.Column('name', mysql.VARCHAR(length=255), nullable=False)) + # ### end Alembic commands ### diff --git a/migrations/versions/b9d22c840958_.py b/migrations/versions/b9d22c840958_.py new file mode 100644 index 0000000..44954d1 --- /dev/null +++ b/migrations/versions/b9d22c840958_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: b9d22c840958 +Revises: 68152efb549c +Create Date: 2022-08-07 23:27:11.651513 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'b9d22c840958' +down_revision = '68152efb549c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('tag', sa.Column('name', sa.String(length=255), nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('tag', 'name') + # ### end Alembic commands ###