Ahmet Bilgin
Ahmet Bilgin

Reputation: 154

sqlalchemy - Creating a table with a dictionary

I'm trying to make a project where the user can also create a table. Initially I was getting tables as json from the user and adding them as a column of a table named application. but from some problems now I have to make the user also create a table directly.

If we come to the question exactly, let's assume that there is such a table.

name = "t_name"    
rows = ["column1","column2","column3"]

how can i convert this to:

t_name = Table(
   't_name', meta, 
   Column('column1', String), 
   Column('column2', String), 
   Column('column3', String), 
)

Upvotes: 0

Views: 89

Answers (1)

Ahmet Bilgin
Ahmet Bilgin

Reputation: 154

I solved the problem in a similar way.

columns_names = ['id','date','name',"username","password"]
columns_types = [Integer,DateTime]
primary_key_flag = [True,False]
for i in columns_names:
    primary_key_flag.append(False)
    columns_types.append(VARCHAR(80))
Table(isim, meta,
            *(Column(column_name, column_type,primary_key = primary_key_flag, column_nullable = True)
            for column_name,
                column_type,
                primary_key_flag in zip(columns_names,
                                        columns_types,
                                        primary_key_flag)))

Upvotes: 1

Related Questions