New to python, flask, and sqlalchemy. Please help.
I'm writing a small application with flask. I have defined some models as:
class Field(db.Model):
__tablename__ = 'fields'
id = db.Column(db.Integer, db.Sequence('FIELDS_SEQ'), primary_key=True)
name = db.Column(db.String(120), nullable=False)
position = db.Column(db.Integer)
# a field has only one type
type_id = db.Column(db.Integer, db.ForeignKey(FieldType.id))
# a field only belongs to one table
table_info_id = db.Column(db.Integer, db.ForeignKey('tables_info.id'), unique=True)
_create_db_sequence('FIELDS_SEQ')
class TableInfo(db.Model):
__tablename__ = 'tables_info'
id = db.Column(db.Integer, db.Sequence('TABLES_INFO_SEQ'), primary_key=True)
name = db.Column(db.String(120), unique=True, nullable=False)
# a table can have a lot of fields
fields = db.relationship(Field, backref='table_info', lazy='joined')
# a table only belongs to one department
department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
_create_db_sequence('TABLES_INFO_SEQ')
So I have a form that will collect the name attribute of TableInfo. Now, how do I dynamically - in runtime- create an empty table with the name attribute given with only one column definition (id). Once table is created, I will use another form to collect field/column names. Again, I'll like to dynamically add these columns to the table. Since this table and its columns won't be based on predefined models as above, how do I create the table and columns in runtime in a way that the application can interact with it? I read that flask-sqlalchemy uses the declarative base which requires models to be defined before mapped tables are created. Can I create tables on the fly in a flask-sqlalchemy environment? Thanks.