From 2896fd4688696ee7775ce422b96191f56efbbc21 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sat, 12 Jun 2021 09:46:21 +0200 Subject: [PATCH] implement skil and language level --- ki/models.py | 49 ++++++++++--------- ...n.py => aebc7e73ad96_initial_migration.py} | 22 +++++---- 2 files changed, 40 insertions(+), 31 deletions(-) rename migrations/versions/{ad03a0d79a90_initial_migration.py => aebc7e73ad96_initial_migration.py} (82%) diff --git a/ki/models.py b/ki/models.py index 39e00ba..231f0b1 100644 --- a/ki/models.py +++ b/ki/models.py @@ -1,19 +1,10 @@ from datetime import datetime -from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Table +from sqlalchemy import Column, Integer, SmallInteger, String, DateTime, ForeignKey from sqlalchemy.orm import relationship from app import db -user_skill_table = Table("user_skill", db.Model.metadata, - Column("user_id", Integer, ForeignKey("user.id")), - Column("skill_id", Integer, ForeignKey("skill.id"))) - -user_language_table = Table( - "user_language", db.Model.metadata, - Column("user_id", Integer, ForeignKey("user.id")), - Column("language_id", String(2), ForeignKey("language.id"))) - class User(db.Model): __tablename__ = "user" @@ -28,12 +19,8 @@ class User(db.Model): contacts = relationship("Contact") address = relationship("Address", uselist=False, back_populates="user") - skills = relationship("Skill", - secondary=user_skill_table, - back_populates="users") - languages = relationship("Language", - secondary=user_language_table, - back_populates="users") + skills = relationship("UserSkill", back_populates="users") + languages = relationship("UserLanguage", "users") class Contact(db.Model): @@ -75,23 +62,41 @@ class Skill(db.Model): id = Column(Integer, primary_key=True) name = Column(String(25), unique=True, nullable=False) - users = relationship("User", - secondary=user_skill_table, - back_populates="skills") + users = relationship("User", back_populates="skills") def to_dict(self): return {"id": self.id, "name": self.name} +class UserSkill(db.Model): + __tablename__ = "user_skill" + + user_id = Column(Integer, ForeignKey("user.id"), primary_key=True) + skill_id = Column(Integer, ForeignKey("skill.id"), primary_key=True) + level = Column(SmallInteger) + + user = relationship("User", back_populates="skills") + skill = relationship("Skill", back_populates="users") + + class Language(db.Model): __tablename__ = "language" id = Column(String(2), primary_key=True) name = Column(String(25), nullable=False) - users = relationship("User", - secondary=user_language_table, - back_populates="languages") + users = relationship("UserLanguage", back_populates="languages") def to_dict(self): return {"id": self.id, "name": self.name} + + +class UserLanguage(db.Model): + __tablename__ = "user_language" + + user_id = Column(Integer, ForeignKey("user.id"), primary_key=True) + language_id = Column(Integer, ForeignKey("language.id"), primary_key=True) + level = Column(SmallInteger) + + user = relationship("User", back_populates="languages") + language = relationship("Language", back_populates="users") diff --git a/migrations/versions/ad03a0d79a90_initial_migration.py b/migrations/versions/aebc7e73ad96_initial_migration.py similarity index 82% rename from migrations/versions/ad03a0d79a90_initial_migration.py rename to migrations/versions/aebc7e73ad96_initial_migration.py index 6075f45..6eed83d 100644 --- a/migrations/versions/ad03a0d79a90_initial_migration.py +++ b/migrations/versions/aebc7e73ad96_initial_migration.py @@ -1,8 +1,8 @@ """Initial migration -Revision ID: ad03a0d79a90 +Revision ID: aebc7e73ad96 Revises: -Create Date: 2021-06-07 21:32:07.388574 +Create Date: 2021-06-12 09:45:28.376639 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'ad03a0d79a90' +revision = 'aebc7e73ad96' down_revision = None branch_labels = None depends_on = None @@ -67,16 +67,20 @@ def upgrade(): sa.PrimaryKeyConstraint('id') ) op.create_table('user_language', - sa.Column('user_id', sa.Integer(), nullable=True), - sa.Column('language_id', sa.String(length=2), nullable=True), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('language_id', sa.Integer(), nullable=False), + sa.Column('level', sa.SmallInteger(), nullable=True), sa.ForeignKeyConstraint(['language_id'], ['language.id'], ), - sa.ForeignKeyConstraint(['user_id'], ['user.id'], ) + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('user_id', 'language_id') ) op.create_table('user_skill', - sa.Column('user_id', sa.Integer(), nullable=True), - sa.Column('skill_id', sa.Integer(), nullable=True), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('skill_id', sa.Integer(), nullable=False), + sa.Column('level', sa.SmallInteger(), nullable=True), sa.ForeignKeyConstraint(['skill_id'], ['skill.id'], ), - sa.ForeignKeyConstraint(['user_id'], ['user.id'], ) + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('user_id', 'skill_id') ) # ### end Alembic commands ###