Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from service import db
- from sqlalchemy import Index
- class UserProfile(db.Model):
- user_id = db.Column(db.String(255), nullable=False, primary_key=True)
- email = db.Column(db.String(255), nullable=False)
- def __init__(self, account_data):
- self.user_id = account_data['userId']
- self.email = account_data['email'].lower()
- def __repr__(self):
- return 'UserID-{}, Email-{}'.format(self.user_id,self.email)
- Index('idx_email', UserProfile.email, unique=True)
- @app.route('/create_account', methods=['POST'])
- def create_account():
- account_data = request.get_json()
- account_details = UserProfile(account_data)
- try:
- db.session.add(account_details)
- db.session.flush()
- # do stuff
- db.session.commit()
- except ProgrammingError as err:
- db.session.rollback()
- if "duplicate key value violates unique constraint "idx_email"" in str(err):
- LOGGER.error('Email address already in use!'
- {
- "userId": "Fred",
- "email": "a@b.com"
- }
- {
- "userId": "Bob",
- "email": "a@b.com"
- }
- sqlalchemy.exc.ProgrammingError: (pg8000.core.ProgrammingError)
- ('ERROR',
- '23505',
- 'duplicate key value violates unique constraint "idx_email"',
- 'Key (email)=(a@b.com) already exists.',
- 'public',
- 'user_profile',
- 'idx_email',
- 'nbtinsert.c',
- '406',
- '_bt_check_unique', '', '')
- [SQL: 'INSERT INTO user_profile (user_id, email) VALUES (%s, %s)']
- [parameters: ('Bob', 'a@b.com')]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement