Zaffiro
Zaffiro

Reputation: 4934

Flask-SqlAlchemy - Foreign key associated with column ‘ ’ could not find table

I am having a weird issue with Flask-SQLAlchemy. I am getting the below error when I try to update a model through a Flask applicaiton, BUT is working fine when executed through the terminal. I am doing a put request (http://localhost:5000/changecontrols/2) through postman. the record is there and like I said works fine from the terminal.

Error: 'Foreign key associated with column 'ChangeControls.client_id' could not find table 'Clients' with which to generate a foreign key to target column ‘id’

My structure looks like this:

I am doing a put request where the application/json type is: { "name": "Data Patch" }

App.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config.from_pyfile('config.py')

db = SQLAlchemy(app)

from webapi import *

if __name__ == '__main__':
    app.run()

WebApi.py

from app import db
@app.route("/changecontrols/<string:id>", methods=["PUT"])
def update_change_control(id):
    data = request.get_json()
    change_control = ChangeControl.query.get(id)

    if change_control is None:
         return jsonify({"result": "change control was not found"}), 404

    change_control.name = data["name"]

   ---> **db.session.commit()  **<- This is where the error occurs****

    return jsonify(change_control.serialize()), 200

ChangeControl

from app import db

class ChangeControl(db.Model):
    __tablename__ = 'ChangeControls'
    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    number = db.Column(db.String(20), nullable=False)
    name = db.Column(db.String(200), nullable=False)
    type = db.Column(db.String(50), nullable=True)
    client_id = db.Column(db.Integer, db.ForeignKey('Clients.id'))

    def load_from_json(self, data):

        self.name = data.get("name",self.name)
        self.number = data.get("number",self.number)
        self.type = data.get("type", self.type)

Client

from app import db

class Client(db.Model):
    __tablename__ = 'Clients'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True )
    name = db.Column(db.String(100), nullable=False)
    startdate = db.Column(db.DateTime , default=datetime.datetime.utcnow())
    inactivedate = db.Column(db.Date, nullable=True)

    changecontrols = db.relationship('ChangeControl', backref='client', lazy='dynamic')

    def load_from_json(self, data):
        self.id = data["id"]
        self.name = data["name"]
        self.number = data["number"]

This same code works in the terminal without an error?!?! enter image description here

Thanks for looking at my post :)

Upvotes: 1

Views: 2570

Answers (1)

renatodamas
renatodamas

Reputation: 19387

That is because you did not import your models in your app.py. Try including the following in your app.py file:

from models.ChangeControl import ChangeControl
from models.Client import Client
from webapi import *

This way you do not need to have all your models in a single package.

Upvotes: 1

Related Questions