boilerplate-code-flask-dashboard
boilerplate-code-flask-dashboard copied to clipboard
Can't create new tables to the database
I tried to create a new class wich build a relationship to the Users table. I deletetd the already excsiting sqlite datatable and rerun the app. But it will just ctreate the pre coded tables. I also tryed to create new tables in /apps/home/models without succcess.
class Users(db.Model, UserMixin): tablename = 'Users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True)
email = db.Column(db.String(64), unique=True)
email_token_key = db.Column(db.String(64))
password = db.Column(db.LargeBinary)
account_status = db.Column(db.Boolean, default=False)
usertypes = db.relationship('Usertype', backref='users', lazy=True)
def __init__(self, **kwargs):
for property, value in kwargs.items():
# depending on whether value is an iterable or not, we must
# unpack it's value (when **kwargs is request.form, some values
# will be a 1-element list)
if hasattr(value, '__iter__') and not isinstance(value, str):
# the ,= unpack of a singleton fails PEP8 (travis flake8 test)
value = value[0]
if property == 'password':
value = hash_pass(value) # we need bytes here (not plain str)
setattr(self, property, value)
def __repr__(self):
return str(self.username)
class Usertype(db.Model): tablename = 'Usertype'
id = db.Column(db.Integer, primary_key=True)
usertype = db.Column(db.String, nullable=False)
users_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
Hello @Robarsc,
The issues cannot be reproduced. Here are the steps to corelate a new table with the existing Users model.
- Check out the latest sources
- Update
apps/authentication/models.pywith the new definition (check below) - Delete
apps/db.sqlite3- if exists - Start the app console via
flask shell - Create the new tables
$ flask shell
>>> from apps import db
>>> db.create_all()
>>> from apps.authentication.models import *
>>> Users.query.all()
[]
>>> Usertype.query.all()
[]
Here is the models.py updated to include the Usertype definition:
class Users(db.Model, UserMixin):
__tablename__ = 'Users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True)
email = db.Column(db.String(64), unique=True)
email_token_key = db.Column(db.String(64))
password = db.Column(db.LargeBinary)
account_status = db.Column(db.Boolean, default=False)
usertypes = db.relationship('Usertype', backref='users', lazy=True)
def __init__(self, **kwargs):
for property, value in kwargs.items():
# depending on whether value is an iterable or not, we must
# unpack it's value (when **kwargs is request.form, some values
# will be a 1-element list)
if hasattr(value, '__iter__') and not isinstance(value, str):
# the ,= unpack of a singleton fails PEP8 (travis flake8 test)
value = value[0]
if property == 'password':
value = hash_pass(value) # we need bytes here (not plain str)
setattr(self, property, value)
def __repr__(self):
return str(self.username)
class Usertype(db.Model, UserMixin):
__tablename__ = 'Usertype'
id = db.Column(db.Integer, primary_key=True)
usertype = db.Column(db.String, nullable=False)
users_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
Let us know yr progress.