https://github.com/shrinivdeshmukh/sqlalchemy-modelgen
Create sqlalchemy python model files by defining tables and columns in a YAML file
https://github.com/shrinivdeshmukh/sqlalchemy-modelgen
database-migration database-schema schema-versions yaml
Last synced: about 1 month ago
JSON representation
Create sqlalchemy python model files by defining tables and columns in a YAML file
- Host: GitHub
- URL: https://github.com/shrinivdeshmukh/sqlalchemy-modelgen
- Owner: shrinivdeshmukh
- License: mit
- Created: 2021-04-01T19:31:31.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-20T06:41:15.000Z (over 4 years ago)
- Last Synced: 2025-09-30T01:25:59.902Z (5 months ago)
- Topics: database-migration, database-schema, schema-versions, yaml
- Language: Python
- Homepage:
- Size: 533 KB
- Stars: 30
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlalchemy-modelgen
[](https://codecov.io/gh/shrinivdeshmukh/sqlalchemy-modelgen)
[](https://pypi.org/project/alchemy-modelgen/)
Create sqlalchemy python model files by defining tables and columns in a yaml file or by specifying database url
# Installation
```
pip install alchemy-modelgen
```
# Usage
**
- Initialize modelgen folder:
**
```
modelgen init -d /path/to/YOUR_FOLDER
cd /path/to/YOUR_FOLDER
```
**
- Create sqlalchemy model code from:
**
**(Option 1)** `yaml` template:
**For details on how to write the yaml file, please follow [docs](https://github.com/shree14/sqlalchemy-modelgen/blob/main/docs/yaml_creation.md)**
```
modelgen createmodel --source yaml --path templates/example.yaml --alembic # path to your schema yaml file
```
**(Option 2)** existing `database`:
```
modelgen createmodel --source database --path mysql+mysqlconnector://root:example@localhost:3306/modelgen --outfile models/YOUR_FILENAME.py --alembic
```
**
- Running alembic migrations:
**
```
modelgen migrate revision --autogenerate -m "COMMIT_MESSAGE" -p mysql+mysqlconnector://root:example@localhost:3306/modelgen
modelgen migrate upgrade head -p mysql+mysqlconnector://root:example@localhost:3306/modelgen
```
The arguments passed after `modelgen migrate` are based on alembic. Any command true for `alembic` can be used with `modelgen migrate`.
**The database url can be passed using `-p` or `--path` argument, or can be set in the environment by the env var `DATABASE_URI`. If `DATABASE_URI` is set, `-p` or `--path` will be ignored**
**
- Alter table support:
**
* Change column type, length, add contraint, etc in the yaml file. Then run:
```
modelgen createmodel --source yaml --path templates/example.yaml --alembic
modelgen migrate revision --autogenerate -m "COMMIT_MESSAGE" -p mysql+mysqlconnector://root:example@localhost:3306/modelgen
modelgen migrate upgrade head -p mysql+mysqlconnector://root:example@localhost:3306/modelgen
```
## Credits
* The code that reads the structure of an existing database and generates the appropriate SQLAlchemy model code is based on [agronholm/sqlacodegen's](https://github.com/agronholm/sqlacodegen) repository (Copyright (c) Alex Grönholm), license: [MIT License](https://github.com/agronholm/sqlacodegen/blob/master/LICENSE)