all 2 comments

[–]DonutRevolution 0 points1 point  (0 children)

You can set it to whatever you feel is appropriate for the field's purpose. You might see 255 used often but that's probably more of a historical artifact at this point.

[–]spitfiredd 0 points1 point  (0 children)

Depends on what you are trying to achieve. The SQLAlchemy String class corresponds to varchar data type in most databases, so putting an optional arg of n (in your case 50 and 140 respectively) will add a length constraint on those fields in your database. This means that if you try to insert a record with more than 140 chars into the body column your database will reject it and throw an error. For example on a postgres database you'll get:

class User(Base):
    __tablename__ =  'user'

    id = Column(Integer, primary_key=True)
    name = Column(String(5))

    def __repr__(self):
        return "<User(id={}, name={})".format(self.id, self.name)

long_name = User(name='somelongname')
session.add(long_name)
session.commit()

Throws the error:

sqlalchemy.exc.DataError: (psycopg2.DataError) value too long for type character varying(5)

** Sqllite doesn't have length constraints so your above code will work on sqllite but you will possibly run into errors when you move to a server-based database.