Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

potykion/attrs_to_sql

Repository files navigation

attrs to sql

Convert attrs class to sql CREATE TABLE command.

Usage

Define class decorated with attr.s:

@attr.s(auto_attribs=True)
class Model:
    id: int = attr.ib(metadata={"primary_key": True})
    name: str = attr.ib(metadata={"not_null": True, "length": 30})
    floats: List[float] = attr.ib(factory=list)

Run attrs_to_table with defined class:

from attrs_to_sql import attrs_to_table

attrs_to_table(Model)

Output:

CREATE TABLE public.model
(
    id int PRIMARY KEY,
    name varchar(30) NOT NULL,
    floats float[]
);