我正在使用Python Eve和SQLAlchemy扩展名,使用MySQL后端创建一个简单的Restful API。
我需要访问并公开位于同一MySQL服务器的两个不同数据库中的两个不同表。我正在使用Multiple Databases with Binds功能,但是它总是尝试连接到默认数据库。
在以下示例中您是否看到任何奇怪的东西?任何见识将受到高度赞赏。
domain.py
"""Multiple bindings configuration.
"""
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.dialects import mysql
from eve_sqlalchemy.config import DomainConfig, ResourceConfig
Base = declarative_base()
class Table2(Base):
__bind_key__ = 'db2'
__tablename__ = "table2"
id = Column(Integer, primary_key=True)
class Table1(Base):
__tablename__ = "table1"
id = Column(String(255), primary_key=True)
DOMAIN = DomainConfig({
'table1': ResourceConfig(Table1),
'table2': ResourceConfig(Table2)
}).render()
settings.py
from domain import DOMAIN
SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@ip:3306/db1'
SQLALCHEMY_BINDS = {
'db2': 'mysql://user:pass@ip:3306/db2'
}
SQLALCHEMY_TRACK_MODIFICATIONS = False
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
DEBUG = True
HATEOAS = False
IF_MATCH = False
PAGINATION = False
EXTRA_RESPONSE_FIELDS = False
ITEMS = 'data'
SQLALCHEMY_ECHO = True
SQLALCHEMY_RECORD_QUERIES = True
app.py
from eve import Eve
from eve_sqlalchemy import SQL
from eve_sqlalchemy.validation import ValidatorSQL
from domain import Base
from settings import on_fetched_resource
app = Eve(validator=ValidatorSQL, data=SQL)
db = app.data.driver
Base.metadata.bind = db.engine
db.Model = Base
# db.create_all()
# using reloader will destroy in-memory sqlite db
app.run(debug=True, use_reloader=False, host='0.0.0.0', port=9003)
$ pip list
Package Version
---------------- -------
Cerberus 0.9.2
Click 7.0
Eve 0.7
Eve-SQLAlchemy 0.7.1
Events 0.2.2
Flask 0.12
Flask-PyMongo 2.3.0
Flask-SQLAlchemy 2.4.1
itsdangerous 0.24
Jinja2 2.10.3
MarkupSafe 0.23
mysqlclient 1.4.4
pip 19.2.3
pymongo 3.9.0
setuptools 41.4.0
simplejson 3.16.0
SQLAlchemy 1.3.9
Werkzeug 0.11.14
wheel 0.33.6
$ python -V
Python 3.6.8
答案 0 :(得分:0)
终于可以正常工作了-非常感谢Dominik Kellner!
domain.py
"""Multiple bindings configuration.
"""
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.dialects import mysql
from eve_sqlalchemy.config import DomainConfig, ResourceConfig
db = SQLAlchemy()
class Table2(db.Model):
__bind_key__ = 'db2'
__tablename__ = "table2"
id = Column(Integer, primary_key=True)
class Table1(db.Model):
__tablename__ = "table1"
id = Column(String(255), primary_key=True)
# The default schema is generated using DomainConfig:
DOMAIN = DomainConfig({
'table1': ResourceConfig(Table1),
'table2': ResourceConfig(Table2)
}).render()
settings.py
from domain import DOMAIN
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
DEBUG = True
HATEOAS = False
IF_MATCH = False
PAGINATION = False
EXTRA_RESPONSE_FIELDS = False
ITEMS = 'data'
SQLALCHEMY_ECHO = True
SQLALCHEMY_RECORD_QUERIES = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@ip:3306/db1'
SQLALCHEMY_BINDS = {
'db2': 'mysql://user:pass@ip:3306/db2'
}
app.py
from eve import Eve
from eve_sqlalchemy import SQL
from domain import db as flask_db
from settings import on_fetched_resource
from eve_sqlalchemy.validation import ValidatorSQL
app = Eve(validator=ValidatorSQL, data=SQL)
db = app.data.driver
flask_db.Model.metadata.bind = db.engine
db.Model = flask_db.Model
# db.create_all()
# using reloader will destroy in-memory sqlite db
app.run(debug=True, use_reloader=False, host='0.0.0.0', port=9003)