Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 314
michael Avatar asked Jan 03 '23 08:01

michael


1 Answers

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?

like image 134
wim Avatar answered Jan 13 '23 12:01

wim