sqlalchemy.exc.InvalidRequestError: Table 'run_result' is already defined for this MetaData instance

Temporary solution:

After the file is imported models db, add the following code:

db.metadata.clear()

But the root of the problem is still the place to find, why would declare the definition of 2nd class it?

 

Resolution:

Table 'roles_users' IS already defined for the this instance the MetaData , meaning that has been defined in Table XX. This is why you want to.

First, if you want to delete the old class and regenerate new mapping class

Then try to run before defining the class the following line:

db.metadata.clear()

The reason is that the first defined by a statement SQLAlchemy Mapping python class, the class definition to be saved in metadata object, define a plurality of maps to prevent conflict caused by the same table. When you call the clear()time method, you will remove all metadata object table definitions stored in memory, which allows you to declare them again.

Second, keep your old class, and then restart your application

You can use reflect methods written test to see whether the table already exists:

db.metadata.reflect(engine=engine)

Which is the database engine that you use to create a connection create_engine()and see if your table already exists, and only when the table is not defined in the definition of the class.

 

Reference: https://stackoverflow.com/questions/37908767/table-roles-users-is-already-defined-for-this-metadata-instance

 

Original Source:

https://blog.csdn.net/BridgeHong/article/details/88999316

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11726336.html