0

I am currently using SQLalchemy with python 3.7 and I was wondering If I could do somehow the following:

eng = create_engine('sqlite:///test.db')
Base = declarative_base()

with open("columns.json", "r") as f:
    columns = json.load(f)

class Tablename(Base):
    __tablename__ = "Tablename"

    IID = Column(Integer, primary_key=True)
    for column, attributes in columns.items():
        '''Add the {column} as instance variable
          so when I create the {Tablename} table
          it will have a column named {column} with
          the type and max_length properties'''

JSON

{
    "Column1": {
        "Type": "Integer",
        "MinLength": "0",
        "MaxLength": "255",
        ...
    },
    {
    "Column2": {
        "Type": "String",
        "MinLength": "0",
        "MaxLength": "50",
        ...
    },
...
}

So, essentially I would like to generate the table from a JSON file. Is it possible?

ish
  • 3
  • 1
  • The accepted answer to the duplicate shows how to create a sqlalchemy from a csv file: you need to follow the same procedure, except getting the values for the table and columns from your JSON structures instead of a csv row (a list). – snakecharmerb Sep 10 '20 at 13:33

0 Answers0