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

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.