-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
如何用tortoise-orm连多个库? #17
Comments
看一下代码 |
# main.py
from rearq import ReArq
from tortoise import Tortoise, connections
rearq = ReArq(db_url='mysql://root:123456@127.0.0.1:3306/rearq')
db_config = {
"connections": {
"data": {
"engine": "tortoise.backends.mysql",
"credentials": {
"database": "data",
"host": "127.0.0.1",
"password": "123456",
"port": 3306,
"user": "root",
},
},
"big_data": {
"engine": "tortoise.backends.mysql",
"credentials": {
"database": "big_data",
"host": "127.0.0.1",
"password": "123456",
"port": 3306,
"user": "root",
},
},
"default": {
"engine": "tortoise.backends.mysql",
"credentials": {
"database": "rearq",
"host": "127.0.0.1",
"password": "123456",
"port": 3306,
"user": "root",
},
},
},
"apps": {
"data": {"models": [], "default_connection": "data"},
"big_data": {"models": [], "default_connection": "big_data"},
"rearq": {"models": ["rearq.server.models"], "default_connection": "default"},
},
"timezone": 'Asia/Shanghai'
}
@rearq.on_shutdown
async def on_shutdown():
# you can do some clean work here like close db and so on...
connections.close_all()
print("shutdown")
@rearq.on_startup
async def on_startup():
# you should do some initialization work here
await Tortoise.init(config=db_config)
print("startup")
@rearq.task(queue="q1")
async def add(self, a, b):
return a + b
#app.py
import uvicorn
from fastapi import FastAPI
from main import rearq
from rearq.server.app import app as rearq_app
app = FastAPI()
app.mount("/rearq", rearq_app)
rearq_app.set_rearq(rearq)
if __name__ == '__main__':
uvicorn.run(app="app:app", host="0.0.0.0", port=8999)
"""
exception:
File "/Users/zhouyuxin/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/rearq/server/routes/index.py", line 26, in index
run_times = await JobResult.all().count()
File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/models.py", line 1277, in all
db = using_db or cls._choose_db()
File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/models.py", line 1033, in _choose_db
db = router.db_for_read(cls)
File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 36, in db_for_read
return self._db_route(model, "db_for_read")
File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 31, in _db_route
return connections.get(self._router_func(model, action))
File "/Users/aaa/Library/Caches/pypoetry/virtualenvs/rearq-test-5bSWUw0w-py3.10/lib/python3.10/site-packages/tortoise/router.py", line 18, in _router_func
for r in self._routers:
TypeError: 'NoneType' object is not iterable
""" |
你试试最新的dev代码 |
解决了,打开方式不对,相关提示留给后来人。 |
好的 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
本项目中,数据库用的是db_url形式传进去的,但是我的项目中连了俩数据库,我尝试在start_up阶段用tortoise.init把我数据库的db_config读进来,但是配置多个数据库后,每个model需要指定app,项目中内置的model没有配置app,就会导致报错。
这里我尝试自己改,但是实在没有思路...
The text was updated successfully, but these errors were encountered: