Answers for "sqlalchemy.exc.InvalidRequestError: Table is already defined for this MetaData instance. Specify 'extend_existing=True' t"

0

sqlalchemy.exc.InvalidRequestError: Table is already defined for this MetaData instance. Specify 'extend_existing=True' t

#I had a similar error.
#My problem was importing the class that inherits db.Model from two different files using a relative import.
#Flask-SQLAlchemy mapped the two imports as two different table definitions and tried to create two tables. 
#I fixed this issue by using the absolute import path in both files.

#BEFORE fix, the imports looked like this:
#Import 1: 
models_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__) + '/models/')))
sys.path.append(models_dir)

from models import VideoFileModel # Import table

#Import 2(In other file):
from app.models.models import VideoFileModel

# AFTER FIX
#Import 1: 
from app.models.models import VideoFileModel # Import table

#Import 2(In other file):
from app.models.models import VideoFileModel
Posted by: Guest on June-28-2021

Code answers related to "sqlalchemy.exc.InvalidRequestError: Table is already defined for this MetaData instance. Specify 'extend_existing=True' t"

Python Answers by Framework

Browse Popular Code Answers by Language