SqlAlchemy Python

huangapple go评论73阅读模式
英文:

SqlAlchemy Python

问题

I'm trying to update two database records in the same function, but I'm always getting this error here:

sqlalchemy.exc.CompileError: Unconsumed column names: skills, github_url, school_records_url, lattes_url, experience, profile, course_certificates_url, course, registration, summary

I did some searching on the internet and thought I should set all the tables, but it didn't work. I did it this way:

def update(self, user_id: str, data: schemas.UpdateUser):
    user = self.db.query(models.User).filter(models.User.id == user_id)
    if not user:
        return 'User not found'
    resume = self.db.query(models.Resume).filter(models.Resume.user_id == user_id)

    resume_data = dict({
        'summary': data.summary,
        'experience': data.experience,
        'skills': data.skills,
        'course': data.course,
        'registration': data.registration,
        'school_records_url': data.school_records_url,
        'profile': data.profile,
        'github_url': data.github_url,
        'lattes_url': data.lattes_url,
        'course_certificates_url': data.course_certificates_url
    })

    db_user = user.first()
    db_resume = resume.first()

    updated_data = data.dict(exclude_unset=False)

    user.filter(models.User.id == user_id).update(updated_data, synchronize_session=False)
    resume.filter(models.Resume.user_id == user_id).update(
        {
            "summary": resume_data['summary'],
            "experience": resume_data['experience'],
            "skills": resume_data['skills'],
            "course": resume_data["course"],
            'registration': resume_data['registration'],
            "school_records_url": resume_data['school_records_url'],
            "profile": resume_data['profile'], "github_url": resume_data['github_url'],
            "lattes_url": resume_data['lattes_url'],
            "course_certificates_url": resume_data['course_certificates_url']}
        , synchronize_session=False)
    self.db.commit()
    self.db.refresh(db_user)
    self.db.refresh(db_resume)
    return {"status": "success"}
英文:

I'm trying to update two database records in the same function, but I'm always getting this error here:

sqlalchemy.exc.CompileError: Unconsumed column names: skills, github_url, school_records_url, lattes_url, experience, profile, course_certificates_url, course, registration, summary

I did some searching on the internet and thought I should set all the tables, but it didn't work. I did it this way:

def update(self, user_id: str, data: schemas.UpdateUser):
    user = self.db.query(models.User).filter(models.User.id == user_id)
    if not user:
        return 'User not found'
    resume = self.db.query(models.Resume).filter(models.Resume.user_id == user_id)

    resume_data = dict({
        'summary': data.summary,
        'experience': data.experience,
        'skills': data.skills,
        'course': data.course,
        'registration': data.registration,
        'school_records_url': data.school_records_url,
        'profile': data.profile,
        'github_url': data.github_url,
        'lattes_url': data.lattes_url,
        'course_certificates_url': data.course_certificates_url
    })

    db_user = user.first()
    db_resume = resume.first()

    updated_data = data.dict(exclude_unset=False)

    user.filter(models.User.id == user_id).update(updated_data, synchronize_session=False)
    resume.filter(models.Resume.user_id == user_id).update(
        {
            "summary": resume_data['summary'],
            "experience": resume_data['experience'],
            "skills": resume_data['skills'],
            "course": resume_data["course"],
            'registration': resume_data['registration'],
            "school_records_url": resume_data['school_records_url'],
            "profile": resume_data['profile'], "github_url": resume_data['github_url'],
            "lattes_url": resume_data['lattes_url'],
            "course_certificates_url": resume_data['course_certificates_url']}
        , synchronize_session=False)
    self.db.commit()
    self.db.refresh(db_user)
    self.db.refresh(db_resume)
    return {"status": "success"}

答案1

得分: 0

您在.update()调用中使用了错误的语法。

或者,至少,您在使用它的方式与我习惯的方式不同。

这是我所做的方式:

resume.filter(Resume.user_id == user_id).update({
    Resume.summary: resume_data['summary'],
    Resume.experience: resume_data['experience'],
    ...
})

字典键应该是实际的模型字段,例如Resume.summary,而不是普通字符串,如"summary"

英文:

You're using the wrong syntax in the .update() call.

Or, at least, you're doing it a different way than I'm used to.

This is how I do it:

resume.filter(Resume.user_id == user_id).update({
    Resume.summary: resume_data['summary'],
    Resume.experience: resume_data['experience'],
    ...
})

The dictionary keys should be actual model fields, like Resume.summary, not plain strings like "summary".

答案2

得分: 0

以下是翻译好的部分:

def update(self, user_id: str, data: schemas.UpdateUser):
    user = self.db.query(models.User).filter(models.User.id == user_id).first()
    if not user:
        return 'User not found'

    resume = self.db.query(models.Resume).filter(models.Resume.user_id == user_id).first()
    if not resume:
        return 'Resume not found'

    user.name = data.name
    user.email = data.email

    resume.summary = data.summary
    resume.experience = data.experience
    resume.skills = data.skills
    resume.course = data.course
    resume.registration = data.registration
    resume.school_records_url = data.school_records_url
    resume.profile = data.profile
    resume.github_url = data.github_url
    resume.lattes_url = data.lattes_url
    resume.course_certificates_url = data.course_certificates_url

    self.db.commit()

    return {"status": "success"}
英文:

I managed to solve it this way:

def update(self, user_id: str, data: schemas.UpdateUser):
    user = self.db.query(models.User).filter(models.User.id == user_id).first()
    if not user:
        return 'User not found'

    resume = self.db.query(models.Resume).filter(models.Resume.user_id == user_id).first()
    if not resume:
        return 'Resume not found'

    user.name = data.name
    user.email = data.email

    resume.summary = data.summary
    resume.experience = data.experience
    resume.skills = data.skills
    resume.course = data.course
    resume.registration = data.registration
    resume.school_records_url = data.school_records_url
    resume.profile = data.profile
    resume.github_url = data.github_url
    resume.lattes_url = data.lattes_url
    resume.course_certificates_url = data.course_certificates_url

    self.db.commit()

    return {"status": "success"}

huangapple
  • 本文由 发表于 2023年3月7日 03:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定