3

I'm creating a flask app which at the moment is small and has one table model created using flask-sqlalchemy.

The app currently imports the table from two separate files while the app is running, and seems to be ok.

The problem is when try to directly run the file I get the following error:

sqlalchemy.exc.InvalidRequestError: Table 'User' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

The model code is:

from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)

Presumably, the model is already created when from app import db is executed. So when line class User(db.Model): is called the error is raised.

I can include __table_args__ = {'extend_existing': True} in the table model which does prevent the error. However, I've read here that this is could be hiding errors.

It is ok to include this line or is there likely to be something wrong with my code?

Many thanks

blountdj
  • 599
  • 1
  • 11
  • 23
  • Have you take a look at https://stackoverflow.com/questions/37908767/table-roles-users-is-already-defined-for-this-metadata-instance ? – PhoneixS May 09 '19 at 20:06

0 Answers0