{"id":14065179,"url":"https://github.com/AlbertoV5/psql-to-models","last_synced_at":"2025-07-29T20:31:29.052Z","repository":{"id":63795931,"uuid":"567118647","full_name":"AlbertoV5/psql-to-models","owner":"AlbertoV5","description":"Convert PostgreSQL schemas to SQLAlchemy and Pydantic Models. ","archived":false,"fork":false,"pushed_at":"2022-11-29T07:56:27.000Z","size":45,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-13T07:08:21.048Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlbertoV5.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-17T05:24:29.000Z","updated_at":"2023-08-17T14:08:46.000Z","dependencies_parsed_at":"2023-01-22T08:00:50.546Z","dependency_job_id":null,"html_url":"https://github.com/AlbertoV5/psql-to-models","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertoV5%2Fpsql-to-models","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertoV5%2Fpsql-to-models/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertoV5%2Fpsql-to-models/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertoV5%2Fpsql-to-models/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlbertoV5","download_url":"https://codeload.github.com/AlbertoV5/psql-to-models/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228046108,"owners_count":17861097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:04:21.058Z","updated_at":"2024-12-04T04:30:50.076Z","avatar_url":"https://github.com/AlbertoV5.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# PSQL TO MODELS\n\nUse regex patterns to match PostgreSQL schemas and output SQLAlchemy and Pydantic Models. \n\nhttps://medium.com/@avq5ac1/creating-python-models-from-sql-tables-with-regex-1e1bfd95ece6\n\nDesigned for FastAPI.\n\n## Install\n\nRequires Python 3.10.\n\n```shell\ngit clone git@github.com:AlbertoV5/psql-to-models.git\n```\n\n```shell\ncd psql-to-models\n```\nInstall in editable mode.\n```shell\npip install -e .\n```\n\n## Usage\n\n```shell\npython -m psql-to-models -i ./example/schema.sql -a ./example/models_alchemy.py -p ./example/models_pydantic.py\n```\nBefore.\n```shell\nschema.sql\n```\nAfter.\n```shell\nmodels_alchemy.py\tschema.sql\nmodels_pydantic.py\n```\n\n## Results Example\n\nSQL Input\n```sql\nDROP TABLE IF EXISTS DATETIMEEVENTS CASCADE;\nCREATE TABLE DATETIMEEVENTS\n(\n  ROW_ID INT NOT NULL,\n\tSUBJECT_ID INT NOT NULL,\n\tHADM_ID INT,\n\tICUSTAY_ID INT,\n\tITEMID INT NOT NULL,\n\tCHARTTIME TIMESTAMP(0) NOT NULL,\n\tSTORETIME TIMESTAMP(0) NOT NULL,\n\tCGID INT NOT NULL,\n\tVALUE TIMESTAMP(0),\n\tVALUEUOM VARCHAR(50) NOT NULL,\n\tWARNING SMALLINT,\n\tERROR SMALLINT,\n\tRESULTSTATUS VARCHAR(50),\n\tSTOPPED VARCHAR(50),\n\tCONSTRAINT datetime_rowid_pk PRIMARY KEY (ROW_ID)\n) ;\n\nDROP TABLE IF EXISTS DIAGNOSES_ICD CASCADE;\nCREATE TABLE DIAGNOSES_ICD\n(\n  ROW_ID INT NOT NULL,\n\tSUBJECT_ID INT NOT NULL,\n\tHADM_ID INT NOT NULL,\n\tSEQ_NUM INT,\n\tICD9_CODE VARCHAR(10),\n\tCONSTRAINT diagnosesicd_rowid_pk PRIMARY KEY (ROW_ID)\n) ;\n```\nSQLALchemy Output\n\n```python\nclass Datetimeevents(Base):\n\n    __tablename__ = \"datetimeevents\"\n\n    row_id = Column(Integer, nullable=False, primary_key=True)\n    subject_id = Column(Integer, nullable=False)\n    hadm_id = Column(Integer)\n    icustay_id = Column(Integer)\n    itemid = Column(Integer, nullable=False)\n    charttime = Column(TIMESTAMP(0), nullable=False)\n    storetime = Column(TIMESTAMP(0), nullable=False)\n    cgid = Column(Integer, nullable=False)\n    value = Column(TIMESTAMP(0))\n    valueuom = Column(String(50), nullable=False)\n    warning = Column(SmallInteger)\n    error = Column(SmallInteger)\n    resultstatus = Column(String(50))\n    stopped = Column(String(50))\n\n\nclass Diagnoses_icd(Base):\n\n    __tablename__ = \"diagnoses_icd\"\n\n    row_id = Column(Integer, nullable=False, primary_key=True)\n    subject_id = Column(Integer, nullable=False)\n    hadm_id = Column(Integer, nullable=False)\n    seq_num = Column(Integer)\n    icd9_code = Column(String(10))\n```\n\nPydantic Output\n\n```python\nclass Datetimeevents(BaseModel):\n\n    row_id: int\n    subject_id: int\n    hadm_id: int | None\n    icustay_id: int | None\n    itemid: int\n    charttime: datetime\n    storetime: datetime\n    cgid: int\n    value: datetime | None\n    valueuom: str\n    warning: int | None\n    error: int | None\n    resultstatus: str | None\n    stopped: str | None\n\n    class Config:\n        orm_mode = True\n\n\nclass Diagnoses_icd(BaseModel):\n\n    row_id: int\n    subject_id: int\n    hadm_id: int\n    seq_num: int | None\n    icd9_code: str | None\n\n    class Config:\n        orm_mode = True\n```\n\n## Supported Queries\n\n```sql\nCREATE TABLE *\n```\n\n```sql\nNOT NULL\n```\n\n```sql\nCONSTRAINT UNIQUE\nCONSTRAINT PRIMARY KEY\n```\n\n## Constants\n\nMake sure to edit the header constants under __ main __.py\n\n```python\nALCHEMY_HEADER = '''\"\"\"\nSQLAlchemy Models\n\"\"\"\nfrom sqlalchemy import Column, Integer, String, CHAR, TIMESTAMP, SmallInteger\nfrom sqlalchemy.dialects.postgresql import DOUBLE_PRECISION\nfrom db.setup import Base\n\n'''\n```\n\nYou can always extend the supported types by editing the TYPE_LOOKUP dict in the types.py file.\n\n```python\nTYPE_LOOKUP: dict[str, tuple[str, str]] = {\n    \"INT\": (\"Integer\", \"int\"),\n    \"SMALLINT\": (\"SmallInteger\", \"int\"),\n    \"VARCHAR\": (\"String\", \"str\"),\n    \"TIMESTAMP\": (\"TIMESTAMP\", \"datetime\"),\n    \"DOUBLE\": (\"DOUBLE_PRECISION\", \"float\"),\n    \"CHAR\": (\"CHAR\", \"str\"),\n    \"TEXT\": (\"String\", \"str\"),\n}\n\"\"\"Values are tuples of SQLAlchemy Model Type and Pydantic/Python Type.\"\"\"\n```\n\n## Notes\n\n- This utility is meant to be modified to match every case that's why the installation is in editable mode.\n- The __ main __ .py file contains all the necessary logic and header configs.\n- The types.py file contains a lookup table for the postgresql -\u003e models type lookup.\n- The header assumes a path for the SQLAlchemy Base so make sure to change it to match yours, etc.\n\n## Plans\n\n- A more robust tool can be created which uses .toml files (or whatever) for configuration instead of python files so there is no need for editable installation.\n- The applications are Postgresql schemas with FastAPI but the tool can be generalized even further to support different types for other RDMS and frameworks.\n- I'll add support for more queries as I find them in my day-to-day work but feel free to contribute!\n\n## Changelog\n\n- 0.1.2 - added ForeignKey Support\n- 0.1.1 - added REAL -\u003e Float support\n- 0.1.0 - initial release\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlbertoV5%2Fpsql-to-models","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAlbertoV5%2Fpsql-to-models","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlbertoV5%2Fpsql-to-models/lists"}