SQLAlchemy - what is declarative_base

后端 未结 1 1597
长发绾君心
长发绾君心 2021-02-02 08:02

I am learning sqlalchemy.
Here is my initial code :

user.py

from sqlalchemy import Column, Integer, Sequence, String
fr         


        
1条回答
  •  庸人自扰
    2021-02-02 08:26

    declarative_base() is a factory function that constructs a base class for declarative class definitions (which is assigned to the Base variable in your example). The one you created in user.py is associated with the User model, while the other one (in main.py) is a different class and doesn't know anything about your models, that's why the Base.metadata.create_all() call didn't create the table. You need to import Base from user.py

    from user import User, Base
    

    instead of creating a new Base class in main.py.

    0 讨论(0)
提交回复
热议问题