flask_sqlalchemy relationship proble,

I have postgres db with some data, i can do everything i want until i add relation to Employee and EmployeeExperience, then a get an error lok like:

InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class EmployeeExperience->employee_experience'. Original exception was: Mapper 'mapped class Employee->employee' has no property 'employee_experience'

I read a lot of tutorials and don't unerstand what i did wrong, any solution doesn't work for me, pls help, here is my code.

import psycopg2
import datetime

# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgres://postgres:postgres@localhost:5433/db_name"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy()
db.init_app(app)
flask_bcrypt = Bcrypt()
flask_bcrypt.init_app(app)
app.app_context().push()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< models
class Employee(db.Model):
    __tablename__ = "employee"
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    surname = db.Column(db.String(200))
    first_name = db.Column(db.String(200))
    birth_date = db.Column(db.Date)
    email = db.Column(db.String(300), unique=True)
    city = db.Column(db.String(200))
    latitude = db.Column(db.Float)
    longitude = db.Column(db.Float)
    male = db.Column(db.Integer)
    registered_on = db.Column(db.DateTime)
    active = db.Column(db.Integer)
    _password = db.Column(db.String(400))
    
    employee_experience = db.relationship("EmployeeExperience", back_populates="employee")
    
    
class EmployeeExperience(db.Model):
    __tablename__ = "employee_experience"
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    position = db.Column(db.String(300))
    company = db.Column(db.String(200))
    city = db.Column(db.String(200))
    description = db.Column(db.Text)
    desc_raw = db.Column(db.Text)
    
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'))
    employee = db.relationship("Employee", back_populates="employee_experience")       
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

emp = Employee.query.first()


Read more here: https://stackoverflow.com/questions/64947449/flask-sqlalchemy-relationship-proble

Content Attribution

This content was originally published by Mateo235711 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: