Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to connect to multiple MySQL DB of same host #147

Closed
nagarajgond opened this issue May 22, 2017 · 7 comments
Closed

how to connect to multiple MySQL DB of same host #147

nagarajgond opened this issue May 22, 2017 · 7 comments
Labels

Comments

@nagarajgond
Copy link

Hi,
I was trying to connect to 2 MySQL DB of same host, I couldn't find any solutions.
I tried with SQLALCHEMY_BINDS with bind_key but it always connects to localhost.

SQLALCHEMY_DATABASE_URI = 'mysql://user:password@host:3306/DB1'
SQLALCHEMY_BINDS = {
'mysql://user:password@host:3306/DB2'
}

@dkellner
Copy link
Collaborator

Can you check if it works by using Flask-SQLAlchemy without Eve-SQLAlchemy? It might be connected to the way we initialize the Base-Model for Flask-SQLAlchemy. Are you using a plain SQLAlchemy declarative base like in our examples? As in https://github.com/pyeve/eve-sqlalchemy/blob/master/examples/tables.py#L10 .

@nagarajgond
Copy link
Author

nagarajgond commented May 30, 2017 via email

@dkellner
Copy link
Collaborator

dkellner commented Jun 2, 2017

Can you clarify your question? Eve / SQLAlchemy will of course use multiple tables for a query if there is a relationship defined.

@luismartingil
Copy link

luismartingil commented Oct 8, 2019

Hi @dkellner ,

I'm also finding it hard to connect to different databases (as referenced here)

Is there anything weird you see in the following example? Any insight will be highly appreciated.


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

@dkellner
Copy link
Collaborator

@luismartingil I've added a working example in PR #199 / Branch multiple-dbs. Can you try if that approach works for you, too?

@luismartingil
Copy link

Working well - thanks

@nagarajgond
Copy link
Author

Sorry @dkellner, I couldn't respond to your question on time.
Looks like it is working :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants