Skip to content

neondatabase-labs/guide-neon-sqlalchemy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running migrations in a Neon-SQLAlchemy project

This application is a simple python API using the FastAPI framework, Neon Postgres database, and SQLAlchemy to interact wiht it. It returns a list of authors and books written by them. We illustrate how to generate and run schema change migrations using the alembic tool.

To build this project from scratch, check out the guide in Neon's documentation.

Set up locally

You will need the following:

  • A Neon account and a project
  • Python 3.8 or higher
  1. Clone this repository:
git clone https://github.com/neondatabase/guide-neon-sqlalchemy
  1. Navigate to the project directory and create a new virtual environment:
cd guide-neon-sqlalchemy
python -m venv myenv
  1. Activate the virtual environment and install the project dependencies:
source myenv/bin/activate
pip install -r requirements.txt
  1. Create a .env file in the root of the project and add the following environment variable with your Neon database URL:
DATABASE_URL=
  1. Run the Alembric migrations:
alembic upgrade head
  1. Run the seed script to populate the database with some data:
python seed.py
  1. Start the FastAPI server:
python main.py
  1. Visit http://localhost:8000 in your browser to see the list of authors and books. Or use curl to access the api from the terminal.
# Get the list of authors
curl http://localhost:8000/authors

# Get books by author with id 1
curl http://localhost:8000/books/1

About

Example guide for Neon with SQL Alchemy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published