Skip to content

Commit

Permalink
Merge pull request #3 from boris/add-author-table
Browse files Browse the repository at this point in the history
Add author table
  • Loading branch information
Boris Quiroz authored Aug 8, 2022
2 parents 752d38e + 83dbfb6 commit ae0b46e
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 36 additions & 0 deletions migrations/versions/11ddd75a4034_.py
Original file line number Diff line number Diff line change
@@ -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 ###
33 changes: 33 additions & 0 deletions migrations/versions/2f3143ca16e9_.py
Original file line number Diff line number Diff line change
@@ -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 ###
39 changes: 39 additions & 0 deletions migrations/versions/5c34faeea863_.py
Original file line number Diff line number Diff line change
@@ -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 ###
44 changes: 44 additions & 0 deletions migrations/versions/68152efb549c_.py
Original file line number Diff line number Diff line change
@@ -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 ###
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified migrations/versions/__pycache__/70f7fdfafc25_.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
28 changes: 28 additions & 0 deletions migrations/versions/a56eb0f88d08_.py
Original file line number Diff line number Diff line change
@@ -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 ###
32 changes: 32 additions & 0 deletions migrations/versions/ab8d661b4728_.py
Original file line number Diff line number Diff line change
@@ -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 ###
28 changes: 28 additions & 0 deletions migrations/versions/b9d22c840958_.py
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit ae0b46e

Please sign in to comment.