'sqlalchemy unique in m:m
I scraped a website resulting in a list of dictionaries. [{'name': name of job}, {'skills': [many skills]}, ...] when using sqlalchemy i built the following:
class Job(Base):
__tablename__ = 'Job'
# Initialize the Column
id = Column(Integer, nullable=False, primary_key=True)
title = Column(String(1000), nullable=False)
# Initialize the relationship
children = relationship("Job_Skills")
class Job_Skills(Base):
__tablename__ = 'Job_Skills'
# Initialize the Column
id = Column(Integer, nullable=False, primary_key=True)
job_id = Column(Integer, ForeignKey('Job.id'))
skill_id = Column(Integer, ForeignKey('Skill.id'))
# Initialize the relationship
skill = relationship('Skill')
class Skill(Base):
__tablename__ = 'Skill'
# Initialize the Column
id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
name = Column(String(100))
for a_dict in dict_merged:
# job
job = Job(title=a_dict["name"]
session.add(job)
# skills
job_Skills = Job_Skills()
session.add(job_Skills)
# add skill in catalogue
for my_skill in [x.strip() for x in a_dict["skills"].split(',')]:
skill = Skill(name=my_skill)
session.add(skill)
the problem is my skill table has many redundancies, I want it to be a uniqe value catalogue that is connected to the proper job via the Job_Skills table.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
