implement skil and language level

This commit is contained in:
weeman 2021-06-12 09:46:21 +02:00
parent 6bedeef48a
commit 2896fd4688
Signed by: weeman
GPG Key ID: 34F0524D4DA694A1
2 changed files with 40 additions and 31 deletions

View File

@ -1,19 +1,10 @@
from datetime import datetime 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 sqlalchemy.orm import relationship
from app import db 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): class User(db.Model):
__tablename__ = "user" __tablename__ = "user"
@ -28,12 +19,8 @@ class User(db.Model):
contacts = relationship("Contact") contacts = relationship("Contact")
address = relationship("Address", uselist=False, back_populates="user") address = relationship("Address", uselist=False, back_populates="user")
skills = relationship("Skill", skills = relationship("UserSkill", back_populates="users")
secondary=user_skill_table, languages = relationship("UserLanguage", "users")
back_populates="users")
languages = relationship("Language",
secondary=user_language_table,
back_populates="users")
class Contact(db.Model): class Contact(db.Model):
@ -75,23 +62,41 @@ class Skill(db.Model):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
name = Column(String(25), unique=True, nullable=False) name = Column(String(25), unique=True, nullable=False)
users = relationship("User", users = relationship("User", back_populates="skills")
secondary=user_skill_table,
back_populates="skills")
def to_dict(self): def to_dict(self):
return {"id": self.id, "name": self.name} 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): class Language(db.Model):
__tablename__ = "language" __tablename__ = "language"
id = Column(String(2), primary_key=True) id = Column(String(2), primary_key=True)
name = Column(String(25), nullable=False) name = Column(String(25), nullable=False)
users = relationship("User", users = relationship("UserLanguage", back_populates="languages")
secondary=user_language_table,
back_populates="languages")
def to_dict(self): def to_dict(self):
return {"id": self.id, "name": self.name} 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")

View File

@ -1,8 +1,8 @@
"""Initial migration """Initial migration
Revision ID: ad03a0d79a90 Revision ID: aebc7e73ad96
Revises: Revises:
Create Date: 2021-06-07 21:32:07.388574 Create Date: 2021-06-12 09:45:28.376639
""" """
from alembic import op from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = 'ad03a0d79a90' revision = 'aebc7e73ad96'
down_revision = None down_revision = None
branch_labels = None branch_labels = None
depends_on = None depends_on = None
@ -67,16 +67,20 @@ def upgrade():
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )
op.create_table('user_language', op.create_table('user_language',
sa.Column('user_id', sa.Integer(), nullable=True), sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('language_id', sa.String(length=2), nullable=True), sa.Column('language_id', sa.Integer(), nullable=False),
sa.Column('level', sa.SmallInteger(), nullable=True),
sa.ForeignKeyConstraint(['language_id'], ['language.id'], ), 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', op.create_table('user_skill',
sa.Column('user_id', sa.Integer(), nullable=True), sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('skill_id', sa.Integer(), nullable=True), sa.Column('skill_id', sa.Integer(), nullable=False),
sa.Column('level', sa.SmallInteger(), nullable=True),
sa.ForeignKeyConstraint(['skill_id'], ['skill.id'], ), 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 ### # ### end Alembic commands ###