0

This is my first model:

class Userauth(db.Model):
    __tablename__ = 'userauth'    

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique = True, nullable = False)
    password = db.Column(db.String(50), nullable = False)

After I made my table in my database, then I'm trying to sign up or create a new user but I got this error:

"Something went wrong: (psycopg2.errors.StringDataRightTruncation) value too long for type character varying(50)"

I realize that I encode the password so it exceeds 50 characters, so I change my code from

password = db.Column(db.String(50), nullable = False)

to

password = db.Column(db.String(150), nullable = False)

But I still got the same error. When I check my database, my table didn't change the char value from 50 to 150. I have migrated and upgrade my database with python manage.py db migrate and upgrade, and there is no error for that but why my table didn't change? Is there any easy way to change or update our table in Flask? (I work with flask-sqlalchemy also), thank you.

Hermawan Wiwid
  • 415
  • 5
  • 14

1 Answers1

1

What if you try to run SQL command directly. Command would be for postgresql something like:

ALTER TABLE Userauth ALTER COLUMN password TYPE varchar(150);

You can run SQL queries with Flask-SqlAlchemy db.session.execute -method. Further information about that: How to execute raw SQL in Flask-SQLAlchemy app

Or it would be even easier if you can access your database console. This is one-time operation, so no need to include it into your applications code.

ex4
  • 2,289
  • 1
  • 14
  • 21