Closed athanhat closed 3 years ago
Hi, I have looked into a relevant issue #88 and I figured out what to do.
In my case all my python code files, etc.. are inside a python package that is called srv_users
, call it app
if you like.
Inside that folder:
I created aerich.ini
[aerich]
tortoise_orm = srv_users.database.TORTOISE_ORM
location = .
and I placed the dictionary variable TORTOISE_ORM inside my database.py, you could place it in config.py if you like
#
# database.py
#
TORTOISE_ORM = { "connections": {"default": "postgres://athan:123@localhost:5432/testdb"}, "apps": { "models": { "models": ["srv_users.models", "aerich.models"], "default_connection": "default" }, }, }
Now I am ready to run aerich commands in the terminal, start with
3. aerich init -t aerich.ini
This is required so that `aerich` knows where to find the TORTOISE_ORM configuration variable. Migration location (.) is also mandatory.
4. aerich init-db
I am running it on a newly created database (testdb)
Success create app migrate location models
Success generate schema for app "models"
This command generates the schema inside my postgres database and creates a models folder according to migrate location.
This is my tree after the execution of (4)
```txt
srv_users
├── aerich.ini
├── api_items.py
├── api_users.py
├── config.py
├── database.py
├── __init__.py
├── main.py
├── models
│ └── 0_20201220215143_init.sql
├── models.py
└── populate.py
tortoise.exceptions.ConfigurationError: Module "models.old_models" not found
It seems I am doing something wrong with the migrate location and the running of init-db and the name, location of my models.py ??? This models.old_models
has been created but it is not found....
There is a hint from the author but I cannot understand what it says...
# If your Tortoise-ORM app is not default models, you must specify --app like
aerich --app other_models init-db.
So perhaps someone can help to solve that last bit and complete this aerich migration example. I recommend that a similar example with the folder tree structure, configuration files, etc is added to tortoise, aerich code base so that others don't loose time to figure out how to run migrations and configure correctly tortoise.
Hey I forgot to say thank you for sharing this great ORM with all of us.
Finally I have found how to do migrations with aerich
using the specific file structure and content that I describe in this issue.
The specific problem here is with the initialisation of aerich
. First delete the aerich.ini
file above and create a new one with the command:
aerich init -t database.TORTOISE_ORM
The content of the new aerich.ini
[aerich]
tortoise_orm = database3.TORTOISE_ORM
location = ./migrations
Now each time you modify Tortoise models in models.py
run aerich migration commands:
aerich migrate aerich upgrade
That now works !
Suggestion: For the author/maintainers of this package:
Include a simple working boilerplate test/example in your aerich release, like the one I described above. BTW it was difficult to find an aerich working example, looking at cookiecutter
and/or docker
deployments. And if possible make concrete examples in the documentation on how to use the aerich init
and aerich -app
commands.
You may also close this issue
Hi, I have read the documentation about Tortoise migration with Aerich but it's not clear what is the format of the file you create with this
TORTOISE_ORM
variable such as the one below, unfortunately reading aerich documentation did not help me to understand how you do the setup. This is what I tried, I created TEXT fileaerich.ini
and then
so can you write an example of a correct config file like the one I am using above. And BTW even this command "riddle" doesn't help a newbie like me :-)
Even better can you share a working boilerplate example with a
models.py
file that includes tortoise models ? That will be great.PS: When I run python scripts, this is my
database.py
file and it generatesaerich models
andtortoise models
tables inside my database fine. But I cannot figure out how to do the same usingaerich
commands in the terminal.