使用没有子类声明基的flask-sqlalchemy [英] using flask-sqlalchemy without the subclassed declarative base

查看:125
本文介绍了使用没有子类声明基的flask-sqlalchemy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flask作为我的python wsgi服务器,sqlalchemy作为我所有的数据库访问权限。

我认为我想使用Flask-Sqlalchemy扩展在我的应用程序,但我不想使用声明基类(db.Model),而是,我想使用sqlalchemy.ext.declarative基地。

这是否破坏了使用扩展的全部目的?




我的用例:

以帮助我管理会话/引擎好一点,但我想分开处理所有模型。$ ​​b
$ b我实际上不会介意使用扩展,但我想写严格模型。我正在从一个非烧瓶应用程序移植代码,并且我将会随着时间的推移将更改推回到该项目。例如,如果flask-sqlalchemy允许我在Table 元数据上作弊,那么当代码被推回时会导致问题。还有我的代码的一部分,做了大量的类型检查(多态身份),我也记得阅读在表上的类型检查不建议使用扩展。



它从flask-sqlalchemy中击败了SQLAlchemy类的整个目的。



以下是一些示例代码:

  from sqlalchemy import * $ b从sqlalchemy.ext.declarative输入$ b从sqlalchemy.orm导入declarative_base 
导入关系,sessionmaker
导入日期时间

#设置sqlalchemy
engine = create_engine('postgresql ://< username>:< password> @ localhost / flask_database')
Base = declarative_base()
metadata = Base.metadata
metadata.bind = engine
Session = sessionmaker(bind = engine,autoflush = True)
session = Session()

$ b $ class User(Base):
__tablename__ ='user'
$ id = Column(Integer,primary_key = True)
api_owner_id = Column(Integer,ForeignKey('api.id'))
email = Column(String(120),unique = True)$ b $ (String(120))
first_name = Column(String(120))
last_name = Column(String(120))
business_name = Column(String 120))
account_type = Column(String (60))
mobile_phone = Column(String(120))
street = Column(String(120))
street2 = Column(String(120))
city = (String(120))
state = Column(String(120))
zip_code = Column(String(120))
country = Column(String(120))
creation_date = Column(DateTime,default = datetime.datetime.now())
password = Column(String(120))
#github stuffs
github_link = Column(Boolean,default = False)
github_usn = Column(String(120))
github_oauth_token = Column(String(160))
#balanced stuffs
balanced_account_uri = Column(String(120))
ach_verified = Column(布尔值,默认值= False)
active = Column(布尔型,默认= True)
profile_updated = Column(布尔值,默认= False)
account_balance = Column(Numeric ,scale = 2),default = 0.00)
admin = Column(布尔值,默认= False)
devapp =关系('DevApp',backref =user,lazy =dy namic)
projects = relationship('Project',backref =user,lazy =dynamic)
proposals = relationship('Proposal',backref =user,lazy =dynamic )
transactions = relationship('Monies',backref =user,lazy =dynamic)
$ b $ def __repr __(self):
return self.email


I am using Flask for my python wsgi server, and sqlalchemy for all my database access.

I think I would like to use the Flask-Sqlalchemy extension in my application, but I do not want to use the declarative base class (db.Model), instead, I want to use the base from sqlalchemy.ext.declarative.

Does this defeat the entire purpose of using the extension?


My use case:

I would like the extension to help me manage sessions/engines a little better, but I would like to handle all models separately.

I actually wouldn't mind using the extension, but I want to write strict models. I am porting code from a non-flask application, and I will be pushing changes back to that project as I go. If flask-sqlalchemy allows me to cheat on Table metadata for instance, that is going to cause problems when the code is pushed back out. There are also portions of my code that do lots of type checking (polymorphic identities), and I also remember reading that type checking on Table is not recommended when using the extension.

解决方案

SQLAlchemy themselves actually recommend you use the Flask wrapper (db.Model) for Flask projects. That being said I have used the declarative_base model in several of my Flask projects where it made more sense.

It does defeat the whole purpose of the SQLAlchemy class from flask-sqlalchemy.

Here's some sample code:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
import datetime

#set up sqlalchemy
engine = create_engine('postgresql://<username>:<password>@localhost/flask_database')
Base = declarative_base()
metadata = Base.metadata
metadata.bind = engine
Session = sessionmaker(bind=engine, autoflush=True)
session = Session()


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    api_owner_id = Column(Integer, ForeignKey('api.id'))
    email = Column(String(120), unique=True)
    username = Column(String(120), unique=True)
    first_name = Column(String(120))
    last_name = Column(String(120))
    business_name = Column(String(120))
    account_type = Column(String(60))
    mobile_phone = Column(String(120))
    street = Column(String(120))
    street2 = Column(String(120))
    city = Column(String(120))
    state = Column(String(120))
    zip_code = Column(String(120))
    country = Column(String(120))
    creation_date = Column(DateTime, default=datetime.datetime.now())
    password = Column(String(120))
    #github stuffs
    github_link = Column(Boolean, default=False)
    github_usn = Column(String(120))
    github_oauth_token = Column(String(160))
    #balanced stuffs
    balanced_account_uri = Column(String(120))
    ach_verified = Column(Boolean, default=False)
    active = Column(Boolean, default=True)
    profile_updated = Column(Boolean, default=False)
    account_balance = Column(Numeric(precision=10, scale=2), default=0.00)
    admin = Column(Boolean, default=False)
    devapp = relationship('DevApp', backref="user", lazy="dynamic")
    projects = relationship('Project', backref="user", lazy="dynamic")
    proposals = relationship('Proposal', backref="user", lazy="dynamic")
    transactions = relationship('Monies', backref="user", lazy="dynamic")

    def __repr__(self):
        return self.email

这篇关于使用没有子类声明基的flask-sqlalchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆