V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-06-20 14:59:29 +08:00 · 1631 次点击
    这是一个创建于 1743 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一般对于一个 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 条回复    2019-06-24 15:22:43 +08:00
    ycz0926
        1
    ycz0926  
       2019-06-20 15:19:49 +08:00
    6167
        2
    6167  
    OP
       2019-06-20 15:45:49 +08:00
    @ycz0926 我瞧了一下感觉和我想要的差距蛮大
    6167
        3
    6167  
    OP
       2019-06-24 15:22:43 +08:00
    这个问题已解决,写配置文件的时候把 user1 加入到 url 中,即
    "url": "oracle+cx_oracle://user1:user1@XXXX:1521/XX"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1036 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 22:29 · PVG 06:29 · LAX 15:29 · JFK 18:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.