table 'roles_users' is already defined for this MetaData instance

前端 未结 11 997
执笔经年
执笔经年 2021-02-05 01:40

hello iam trying to get a currnet_user information in my views

and i include from users.models import *

then in my code return return current_user;

<         


        
11条回答
  •  爱一瞬间的悲伤
    2021-02-05 02:03

    This depends on what you want to do, are you trying to:

    1. delete the old class and repalce it with a new class mapping? Or,
    2. Keep the old class and just restart your app?

    In the first case using your example above, try running the following line before defining your classes:

    db.metadata.clear()
    

    The reason is the first time you declare a SQLAlchemy Mapping by defining a python class, the definition of the class is saved to the metadata object, in order to prevent conflicts caused by multiple definitions being mapped to the same table.

    When you call the clear() method you clear all the table definitions held in memory by the Metadata object, which allows you to declare them again.

    In the second case when you're just restarting the app I would write a test to see if the table already exists using the reflect method:

    db.metadata.reflect(engine=engine)
    

    Where engine is your db connection created using create_engine(), and see if your tables already exist and only define the class if the table is undefined.

提交回复
热议问题