Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. from service import db
  2. from sqlalchemy import Index
  3.  
  4. class UserProfile(db.Model):
  5. user_id = db.Column(db.String(255), nullable=False, primary_key=True)
  6. email = db.Column(db.String(255), nullable=False)
  7.  
  8. def __init__(self, account_data):
  9. self.user_id = account_data['userId']
  10. self.email = account_data['email'].lower()
  11.  
  12. def __repr__(self):
  13. return 'UserID-{}, Email-{}'.format(self.user_id,self.email)
  14.  
  15. Index('idx_email', UserProfile.email, unique=True)
  16.  
  17. @app.route('/create_account', methods=['POST'])
  18. def create_account():
  19.  
  20. account_data = request.get_json()
  21.  
  22. account_details = UserProfile(account_data)
  23. try:
  24. db.session.add(account_details)
  25. db.session.flush()
  26.  
  27. # do stuff
  28.  
  29. db.session.commit()
  30.  
  31. except ProgrammingError as err:
  32. db.session.rollback()
  33. if "duplicate key value violates unique constraint "idx_email"" in str(err):
  34. LOGGER.error('Email address already in use!'
  35.  
  36. {
  37. "userId": "Fred",
  38. "email": "a@b.com"
  39. }
  40.  
  41. {
  42. "userId": "Bob",
  43. "email": "a@b.com"
  44. }
  45.  
  46. sqlalchemy.exc.ProgrammingError: (pg8000.core.ProgrammingError)
  47. ('ERROR',
  48. '23505',
  49. 'duplicate key value violates unique constraint "idx_email"',
  50. 'Key (email)=(a@b.com) already exists.',
  51. 'public',
  52. 'user_profile',
  53. 'idx_email',
  54. 'nbtinsert.c',
  55. '406',
  56. '_bt_check_unique', '', '')
  57. [SQL: 'INSERT INTO user_profile (user_id, email) VALUES (%s, %s)']
  58. [parameters: ('Bob', 'a@b.com')]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement