Flask SQLAlchemy throws "NoReferencedTableError" when arranging codes in package structure - python

I am learning flask and trying to arrange my codes in the "package structure" according to some online tutorials. I have put together some minimal codes to replicate my problem.
First the tree structure of my codes is
.
└── test001
├── __init__.py
├── models.py
└── views.py
The source codes for each file:
"__init__.py":
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'somerandomnumber'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
from test001 import views
Source for "models.py":
from test001 import db,app
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
favoritejob_id = db.Column(db.Integer, db.ForeignKey('favoritejob.id'), nullable=True)
favoritejob = db.relationship("FavoriteJob", backref=db.backref("users"), lazy=True)
class FavoriteJob(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), nullable=False)
def __repr__(self):
return f"<FavoriteJob ID={self.id} Name={self.name}>"
Source for "views.py" (putting all routes together):
from test001.models import User, FavoriteJob
# #app.route("/register", methods=['GET', 'POST'])
# def register():
# PLACE HOLDER FOR USER REGISTRATION
With the codes above, I then go to python3 shell and to the following:
from test001 import db
db.create_all()
and I get the following errors:
---------------------------------------------------------------------------
NoReferencedTableError Traceback (most recent call last)
<ipython-input-2-653042798025> in <module>
----> 1 db.create_all()
...
...
~/.local/lib/python3.6/site-packages/sqlalchemy/sql/schema.py in column(self)
2045 "foreign key to target column '%s'"
2046 % (self.parent, tablekey, colname),
-> 2047 tablekey,
2048 )
2049 elif parenttable.key not in parenttable.metadata:
NoReferencedTableError: Foreign key associated with column 'user.favoritejob_id' could not find table 'favoritejob' with which to generate a foreign key to target column 'id'
I did quite a bit searching about this error but couldn't find anything wrong with models.py. Then I removed the following line from views.py, which makes views.py an empty file
from test001.models import User, FavoriteJob
Then "db.create_all()" starts working.
However I can't leave views.py empty as I will put all the views/url routes in the file and will need to use the User and FavoriteJob classes.
I couldn't figure out why the import line in views.py would cause db.create_all() to fail. I am quite new to flask and sqlalchemy so sorry if there is something obviously wrong with my setup. I appreciate your help.

Flask-SQLAlchemy defines the table names from descriptive base models (unless you've defined one yourself). And if a model name is e.g. CamelCase, it sets the table name as camel_case, not camelcase.
In your model definitions, you have a related field on User model -- favoritejob_id, which is related to FavoriteJob model (as per the relationship declaration favoritejob). As you don't have any explicit table name for the FavoriteJob model, Flask-SQLAlchemy will name it as favorite_job. But your related field definition has:
db.ForeignKey('favoritejob.id')
as the table favoritejob does not exist, you're getting the error (as expected).
You have a couple of options to handle this:
The easiest option probably is to rename the related table name:
db.ForeignKey('favorite_job.id')
Or give the table name for FavoriteJob model as favoritejob:
class FavoriteJob(db.Model):
__tablename__ = 'favoritejob'

Related

How to map table relationships with flask-sqlalchemy, before tables exist?

I have a problem creating relationships between my tables in flask-sqlalchemy. I have a table with project overview, and from there on out I want to dynamically create new experiment tables with a relationship to my project overview. However, when I try to define the relationship, sqlalchemy throws the following error:
sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Projects->projects, expression 'Experiment_Overview' failed to locate a name ('Experiment_Overview'). If this is a c
lass name, consider adding this relationship() to the <class 'app.Projects'> class after both dependent classes have been defined.
This seems to be the case because the class Experiment_Overview(db.Model) does not exist yet, which is correct since it will be dynamically generated later on through user input. How can I mitigate this error?
import os
from flask import Flask, render_template, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
Bootstrap(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///DATA/DB.db"
db = SQLAlchemy(app)
def TableCreator(tablename):
class Experiment_Overview(db.Model):
__tablename__ = tablename
creation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
experiment_name = db.Column(db.String(30), unique=False, nullable=False, primary_key=True)
projectname = db.Column(db.String(150), db.ForeignKey('projects.projectname'), nullable=False, unique=True)
return MyTable
class Projects(db.Model):
projectname = db.Column(db.String(150), unique=True, nullable=False, primary_key=True)
creation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
experiments = db.relationship('Experiment_Overview', backref="experiments", lazy=True, uselist=False)
def __init__(self, owner, projectname, status, created_at):
self.owner=owner
self.projectname=projectname
self.status=status
self.created_at=created_at
db.create_all()
Generally speaking, you don't dynamically create tables; you usually shouldn't create or drop tables while your program is running.
Additionally, I believe it's impossible to create a true relationship that links back to an entire table. Relationships/Foreign Keys are for linking between rows in tables.
Don't worry, there are easier ways to achieve the behavior that you are looking for here.
From your question it sounds like you can have multiple Projects, and each project can have multiple Experiments within the project.
This would make the relationship between a Project and its Experiments a One-To-Many relationship.
If this is the case, you would need one Projects table (which you have already in your code), and you would also have one Experiments table.
Each row in the Projects table represents one Project.
Each row in the Experiments table represents one Experiment. The Experiments table will have a column containing a foreign key linking back to the Project the Experiment is linked to.
I've modified your code according to the One-To-Many example code given in the SQLAlchemy documentation that I linked above.
Note the addition of the back_populates option to the relationship(), this allows bi-directional knowledge of the relationship: the Experiment know what project it belongs to, and the Project know what Experiments it has.
import os
from flask import Flask, render_template, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
Bootstrap(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///DATA/DB.db"
db = SQLAlchemy(app)
class Projects(db.Model):
__tablename__ = "projects"
projectname = db.Column(db.String(150), unique=True, nullable=False, primary_key=True)
creation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
experiments = db.relationship("Experiment", back_populates="project")
def __init__(self, owner, projectname, status, created_at):
self.owner=owner
self.projectname=projectname
self.status=status
self.created_at=created_at
class Experiment(db.Model):
__tablename__ = "experiments"
creation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
experiment_name = db.Column(db.String(30), unique=False, nullable=False, primary_key=True)
projectname = db.Column(db.String(150), db.ForeignKey('projects.projectname'), nullable=False)
project = relationship("Project", back_populates="experiments")
db.create_all()

Sql alchemy sqlalchemy.exc.NoReferencedTableError not creating tables

I'm working with flask and trying to learn sqlalchemy, I have a database where I added a third table to make a new feature, but the entire project stopped working, it says:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'url_reference.folder_rel' could not find table 'reffolders' with which to generate a foreign key to target column 'id'
this is the dbs.py
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(200), unique=True)
user_password = db.Column(db.String(150))
user_name = db.Column(db.String(400))
user_refs = db.relationship("RefFolders")
class RefFolders(db.Model):
id = db.Column(db.Integer, primary_key=True)
folder_title = db.Column(db.String(1000))
date_added = db.Column(db.DateTime(timezone=True), default=func.now())
user_rel = db.Column(db.Integer, db.ForeignKey("user.id"))
url_rel = db.relationship("UrlReference")
class UrlReference(db.Model):
id = db.Column(db.Integer, primary_key=True)
ref_title = db.Column(db.String(1000))
ref_url = db.Column(db.String(30000))
date_added = db.Column(db.DateTime(timezone=True), default=func.now())
folder_rel = db.Column(db.Integer, db.ForeignKey("reffolders.id"))
this is where I make the database in the __init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
db = SQLAlchemy()
DB_NAME = "database.db"
def app():
app = Flask(__name__)
app.config['SECRET_KEY'] = '<here is the key in my code>'
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{DB_NAME}"
db.init_app(app)
# making the blueprints usable
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix="/")
app.register_blueprint(auth, url_prefix="/")
from .dbs import User
from .dbs import UrlReference
from .dbs import RefFolders
db_create(app)
# Login manager
login_manager= LoginManager()
login_manager.login_view = 'views.home'
login_manager.init_app(app)
# Searching and loading user
#login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
return app
def db_create(app):
if not path.exists("project/" + DB_NAME):
db.create_all(app=app)
I don't know what happened, maybe a problem with having a double foreign key?
I think flask or its extension guesses the table name you want, like CamelCase --> camel_case.
So in this case the tablename is probably ref_folders. You might want to just use tablename to set the table name yourself although I think their default is a good convention.
As a side note I would recommend using _id for your ids otherwise things might get pretty confusing between object relations and actual ids, ie. use user_id instead of user_rel in RefFolders.
SEE:
https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/#declaring-models
Some parts that are required in SQLAlchemy are optional in
Flask-SQLAlchemy. For instance the table name is automatically set for
you unless overridden. It’s derived from the class name converted to
lowercase and with “CamelCase” converted to “camel_case”. To override
the table name, set the __tablename__ class attribute.

Import just in root file but not in helper files Python

I am building a Flask API with a SQLAlchemy database. In my app.py file (the main file) I have all the necessary imports and setups, like so:
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
db = SQLAlchemy(app)
Th problem is, I would like to move this model
class User(db.Model):
id = db.Column(db.integer, primary_key=True)
username = db.Column(db.String(20))
password = db.Column(db.String(16))
to a separate file, to keep everything neat, but it inherits from the import, and from the
app = Flask(__name__)
, but I cant have two of those, because the app crashes. In javascript for example, it is ppossible to declare a function that requires certain imports without importing them in that same file, as long as that function gets exported to another file where these requirements are met. In python though, it throws a db is not defined error. Any idea how to fix this?
You did not mention to the table name in User model. It should be
class User(db.Model):
__tablename__ = 'user_table_name'
id = db.Column(db.integer, primary_key=True)
username = db.Column(db.String(20))
password = db.Column(db.String(16))
replace user_table_name with the actual name of the table in your SQLite database.

Flask-SQLAlchemy One to Many relationship failed to locate a name

I have a Flask app with Flask-SQLAlchemy. I'm trying to make a one to many relationship from a user to a trip. I saw that, if I use the declarative_base of SQLAlchemy it works but when I use the db.Model from Flask-SQLAlchemy it dosen't work.
I'm getting the following error:
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|User|User, expression 'Trip' failed to locate a name ("name 'Trip' is not defined"). If this is a class name, consider adding this relationship() to the <class 'models.user.User'> class after both dependent classes have been defined.
Here are the models:
database.py
from flask import abort
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc
db = SQLAlchemy()
class Mixin(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, default=db.func.now())
date_changed = db.Column(
db.DateTime, nullable=True, onupdate=db.func.now())
date_to = db.Column(db.DateTime, nullable=True)
user.py
from database import Mixin, db
class User(Mixin, db.Model):
__tablename__ = 'User'
trips = db.relationship(
'Trip',
backref=db.backref('user', lazy='joined'),
lazy='dynamic'
)
trip.py
from database import Mixin, db
class Trip(Mixin, db.Model):
__tablename__ = 'Trip'
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(250), nullable=False)
user_id = db.Column(
db.Integer,
db.ForeignKey('User.id'),
nullable=False
)
Storing all models in one file is a good idea, but only for a small number of models. For the large I would prefer to store models in semantically separate files.
models
├── __init__.py
├── model1.py
├── model2.py
└── model3.py
In the __init__.py need to import all models files
import models.model1.py
import models.model2.py
import models.model3.py
I fixed it by using one file for my models. I didn't have enough models to need multiple model files anyway.

Getting Flask-Migrate to Ignore SQL Views that are mapped as Flask-SQLAlchemy Models

I am using Flask-SQLAlchemy to define my models, and then using Flask-Migrate to auto-generate migration scripts for deployment onto a PostgreSQL database. I have defined a number of SQL Views on the database that I use in my application like below.
However, Flask-Migrate now generates a migration file for the view as it thinks it's a table. How do I correctly get Flask-Migrate / Alembic to ignore the view during autogenerate?
SQL View name: vw_SampleView with two columns: id and rowcount.
class ViewSampleView(db.Model):
__tablename__ = 'vw_report_high_level_count'
info = dict(is_view=True)
id = db.Column(db.String(), primary_key=True)
rowcount = db.Column(db.Integer(), nullable=False)
Which means I can now do queries like so:
ViewSampleView.query.all()
I tried following instructions on http://alembic.zzzcomputing.com/en/latest/cookbook.html and added the info = dict(is_view=True) portion to my model and the following bits to my env.py file, but don't know where to go from here.
def include_object(object, name, type_, reflected, compare_to):
"""
Exclude views from Alembic's consideration.
"""
return not object.info.get('is_view', False)
...
context.configure(url=url,include_object = include_object)
I think (though haven't tested) that you can mark your Table as a view with the __table_args__ attribute:
class ViewSampleView(db.Model):
__tablename__ = 'vw_report_high_level_count'
__table_args__ = {'info': dict(is_view=True)}
id = db.Column(db.String(), primary_key=True)
rowcount = db.Column(db.Integer(), nullable=False)

Categories