推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
6167
V2EX  ›  Python

flask 项目中 sqlalchemy 与 oracle 多用户访问求解?

  •  
  •   6167 · Jun 20, 2019 · 2632 views
    This topic created in 2661 days ago, the information mentioned may be changed or developed.

    一般对于一个 orm 框架的项目来说,模型代码

    class User(db.Model):
        __tablename__ = 'user'
        id = Column(BigInteger, primary_key=True)
        a = Column(String)
    

    最终程序会发给数据库 SQL 代码

    SELECT user.id AS user_id, user.a AS user_a 
    FROM user
    

    但是在 oracle 中,user 表前面存在一个数据库的用户概念(比如说 user1 ),所以,必须发送如下代码才能获取数据

    SELECT user.id AS user_id, user.a AS user_a 
    FROM user1.user
    

    但是实际上,按照

    https://www.oschina.net/question/237257_37964 提供的方案,模型更改为

    class User(db.Model):
        __tablename__ = 'user1.user'
        __table_args__ = {'quote': False}
        id = Column(BigInteger, primary_key=True)
        a = Column(String)
    

    实际上程序给数据库发送的 SQL 代码是

    SELECT user1.user.id AS user1.user_id, user1.user.a AS user1.user_a 
    FROM user1.user
    

    这样的代码是获取不到数据的

    还需要什么东西才能让程序发送正确的代码?

    SELECT user.id AS user_id, user.a AS user_a 
    FROM user1.user
    
    3 replies  •  2019-06-24 15:22:43 +08:00
    ycz0926
        1
    ycz0926  
       Jun 20, 2019
    6167
        2
    6167  
    OP
       Jun 20, 2019
    @ycz0926 我瞧了一下感觉和我想要的差距蛮大
    6167
        3
    6167  
    OP
       Jun 24, 2019
    这个问题已解决,写配置文件的时候把 user1 加入到 url 中,即
    "url": "oracle+cx_oracle://user1:user1@XXXX:1521/XX"
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2529 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 811ms · UTC 14:37 · PVG 22:37 · LAX 07:37 · JFK 10:37
    ♥ Do have faith in what you're doing.