3

The flask-sqlalchemy documentation is very short, and shows how to create all the tables from a file with classes representing those tables.

a table class is something like this :

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLalchemy(app)

class Table(db.Model):
    id = db.Column(db.Integer)
    name = db.Column(db.String)

db.create_all()

This will create a table into whatever database is specified with the 'app' config

What I wanna do is to create tables using a simple form

how can I create a single table dynamically ?

something like

def createTable(column1,column2):
    #create table
T.Galisnky
  • 79
  • 1
  • 11
  • 1
    Some reading to get started: https://stackoverflow.com/questions/973481/dynamic-table-creation-and-orm-mapping-in-sqlalchemy, https://stackoverflow.com/questions/43721680/sqlalchemy-create-dynamic-tables-and-columns, https://stackoverflow.com/questions/25225320/dynamically-creating-a-set-of-sqlalchemy-tables – Ilja Everilä Oct 30 '19 at 16:27
  • 1
    Thank you very much, all of those question speak about bare sqlalchemy which is a a little differenet from flask-sqlalchemy, I don't want to use both as my code might get messy that way, so i'm trying to stick to flask-sqlalchemy, and i believe i've found a solution by creating a class dynamically and then call db.create_all() just afterwards, I'd like to create just that peticular table but it seems that function does not exist in flask-sqlalchemy that i know of, and create_all() does not affect other already created tables, so there's that .. – T.Galisnky Oct 30 '19 at 16:49

0 Answers0