Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sql.py #115

Closed
wants to merge 1 commit into from
Closed

Update sql.py #115

wants to merge 1 commit into from

Conversation

igorvoltaic
Copy link

Functionality which commits changes to Postgres DB
https://www.postgresql.org/docs/8.3/sql-commit.html

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Functionality which commits changes to Postgres DB
https://www.postgresql.org/docs/8.3/sql-commit.html
@kzidane
Copy link
Member

kzidane commented Apr 8, 2020

What are you trying to do exactly, @igorvoltaic?

@igorvoltaic
Copy link
Author

igorvoltaic commented Apr 8, 2020

Hi Kareem,

I've been following the guide to deploy my app to Heroku and spent quite some time trying to understand why non of the changes to Prostges are persistent

Here is an example:


Python 3.7.6 (default, Mar  6 2020, 22:15:44) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cs50 import SQL
>>> db = SQL("postgres://URI?sslmode=require")
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[]
>>> db.execute("INSERT INTO warehouses (name, address, status) VALUES (:name, :address, :status);", name='wh-1', address='add-1', status=1)
1
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[{'id': 1, 'name': 'wh-1', 'address': 'add-1', 'status': 1}]
>>> 
>>> 
>>> 
>>> 
>>> db = SQL("postgres://URI?sslmode=require")
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[]
>>>

Turns out you need to issue COMMIT statement for changes to be persistent in Postgres.

Also Travis tells me I need to do more testing first :)

@igorvoltaic
Copy link
Author

Just checked the lib (sql.py) locally, everything works as intended:

Python 3.7.6 (default, Mar  6 2020, 22:15:44) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sql import SQL
>>> db = SQL("postgres://URI?sslmode=require")
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[]
>>> db.execute("INSERT INTO warehouses (name, address, status) VALUES (:name, :address, :status);", name='wh-1', address='add-1', status=1)
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[{'id': 2, 'name': 'wh-1', 'address': 'add-1', 'status': 1}]
>>> 
>>> 
>>> 
>>>
>>> db = SQL("postgres://URI?sslmode=require")
>>> warehouses = db.execute("SELECT id, name, address, status FROM warehouses")
>>> warehouses
[{'id': 2, 'name': 'wh-1', 'address': 'add-1', 'status': 1}]
>>> 

@dmalan
Copy link
Member

dmalan commented Jun 12, 2020

Thank you for catching this, @igorvoltaic. We're about to deploy a broader set of changes to handle transactions more robustly!

#122

@dmalan dmalan closed this Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants