michael
michael

Reputation: 113

Flask-SQLAlchemy whats the difference db.Column vs Column

So I am making new flask app and noticed even in other projects that db.Column and Column is used interchangeably. So when do you use one over the other?

id = db.Column(Integer(), primary_key=True)

vs

id = Column(Integer(), primary_key=True)

Upvotes: 4

Views: 1505

Answers (1)

wim
wim

Reputation: 362647

It appears to be a convenience; there is no difference.

>>> from flask_sqlalchemy import SQLAlchemy
>>> db = SQLAlchemy()
>>> from sqlalchemy import Column
>>> db.Column is Column
True

In the docstring of the class SQLAlchemy you may see this comment:

This class also provides access to all the SQLAlchemy functions and classes from the sqlalchemy and sqlalchemy.orm modules.

The names are provided by a helper function _include_sqlalchemy that get's called in SQLAlchemy's initializer. A questionable design choice, perhaps, disregarding zen of Python #13 for no reason other than to reduce the number of import statements at the top of the module?

Upvotes: 11

Related Questions