0

Background: I am using Heroku to deploy a website using postgresql and gunicorn. It keeps throwing me a null error, and I'm not sure how to proceed. The database is simply a primary key ID, the username, password(hashed) and the users cash amount. Please let me know if there are additional details needed.

2022-01-07T18:37:46.209593+00:00 app[web.1]: DEBUG: [33mINSERT INTO users(username, hash) VALUES ('bells', 'pbkdf2:sha256:260000$3HbHmQOV4bCNLi5u$c8fe4a97274677b4465789748c649a95d900e7924916f1b5af95b4d9e111d5a8')[0m
2022-01-07T18:37:46.212392+00:00 app[web.1]: ERROR: Exception on /register [POST]
2022-01-07T18:37:46.212393+00:00 app[web.1]: [33mTraceback (most recent call last):[0m
2022-01-07T18:37:46.212394+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
2022-01-07T18:37:46.212395+00:00 app[web.1]:     response = self.full_dispatch_request()
2022-01-07T18:37:46.212396+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
2022-01-07T18:37:46.212396+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2022-01-07T18:37:46.212396+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
2022-01-07T18:37:46.212397+00:00 app[web.1]:     rv = self.dispatch_request()
2022-01-07T18:37:46.212397+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
2022-01-07T18:37:46.212397+00:00 app[web.1]:     return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2022-01-07T18:37:46.212398+00:00 app[web.1]:   File "/app/app.py", line 282, in register
2022-01-07T18:37:46.212398+00:00 app[web.1]:     db.execute("INSERT INTO users(username, hash) VALUES (?, ?)", username, password)
2022-01-07T18:37:46.212399+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/cs50/sql.py", line 27, in decorator
2022-01-07T18:37:46.212400+00:00 app[web.1]:     return f(*args, **kwargs)
2022-01-07T18:37:46.212400+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/cs50/sql.py", line 374, in execute
2022-01-07T18:37:46.212401+00:00 app[web.1]:     raise e
2022-01-07T18:37:46.212401+00:00 app[web.1]: [33mValueError: null value in column "id" of relation "users" violates not-null constraint
2022-01-07T18:37:46.212402+00:00 app[web.1]: DETAIL:  Failing row contains (null, bells, pbkdf2:sha256:260000$3HbHmQOV4bCNLi5u$c8fe4a97274677b4465789748c..., 10000.00).[0m
2022-01-07T18:37:46.213381+00:00 app[web.1]: 10.1.17.237 - - [07/Jan/2022:18:37:46 +0000] "POST /register HTTP/1.1" 500 2556 "https://financestocks.herokuapp.com/register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2022-01-07T18:37:46.212399+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/cs50/sql.py", line 27, in decorator
2022-01-07T18:37:46.212400+00:00 app[web.1]:     return f(*args, **kwargs)
2022-01-07T18:37:46.212400+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/cs50/sql.py", line 374, in execute
2022-01-07T18:37:46.212401+00:00 app[web.1]:     raise e
2022-01-07T18:37:46.212401+00:00 app[web.1]: [33mValueError: null value in column "id" of relation "users" violates not-null constraint
2022-01-07T18:37:46.212402+00:00 app[web.1]: DETAIL:  Failing row contains (null, bells, pbkdf2:sha256:260000$3HbHmQOV4bCNLi5u$c8fe4a97274677b4465789748c..., 10000.00).[0m
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    What datatype is the `id` column? Did you declare it as `SERIAL` or otherwise create a sequence to generate values for it? – ChrisGPT was on strike Jan 07 '22 at 19:20
  • Note that this has nothing to do with Heroku or Gunicorn. It's a straight PostgreSQL database issue. – ChrisGPT was on strike Jan 07 '22 at 19:21
  • I'm new to programming, but here is how I created the table ( CREATE TABLE 'users' ('id' integer PRIMARY KEY NOT NULL, 'username' text NOT NULL, 'hash' text NOT NULL, 'cash' numeric NOT NULL DEFAULT 10000.00 )) My intention for the id is that it has to be unique for each user, and as long as the ID keeps autoincrementing when inserted, it should be unique as users sign up – Francisco Gutierrez Ramirez Jan 07 '22 at 19:22
  • @Chris noted, I wasn't sure what I was dealing with – Francisco Gutierrez Ramirez Jan 07 '22 at 19:22
  • I think I need to rethink the way I am creating the ID part of the database, or figure out how to make it autoincrement, perhaps using SERIAL? – Francisco Gutierrez Ramirez Jan 07 '22 at 19:28
  • 2
    Put the table definition into the question if you want to keep it open. It's an integral part. (Though the single quotes make no sense, must be translated incorrectly somehow.) You already have your answer, though: `id` is required, but not provided. Consider a `serial` or `IDENTITY` column instead. See: https://stackoverflow.com/a/9875517/939860 – Erwin Brandstetter Jan 07 '22 at 19:30
  • @ErwinBrandstetter I will look into it. I think it could also be an issue of converting from originally a SQL database to postgresql. I used pgloader to convert. – Francisco Gutierrez Ramirez Jan 07 '22 at 19:35
  • It works now, it was an issue of converting, and missing AUTO INCREMENT section when defining the ID. I'm not sure how to close this question, or say it was answered – Francisco Gutierrez Ramirez Jan 07 '22 at 19:40
  • I closed it as duplicate of the auto increment issue. – Erwin Brandstetter Jan 07 '22 at 23:38

0 Answers0