You can download this code by clicking the button below.
This code is now available for download.
This function is used to create a new user record while ensuring that the email length does not exceed 255 characters. If the length exceeds, a ValueError is raised.
Technology Stack : flask_sqlalchemy
Code Type : Function
Code Difficulty : Intermediate
from flask_sqlalchemy import SQLAlchemy, Column, Integer, String
from sqlalchemy.sql import func
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(80), unique=True, nullable=False)
email = Column(String(120), unique=True, nullable=False)
def create_user_with_max_email_length(username, email):
if len(email) > 255:
raise ValueError("Email length should not exceed 255 characters")
new_user = User(username=username, email=email)
db.session.add(new_user)
db.session.commit()