mikeizbicki / cmc-csci143

big data course materials
40 stars 76 forks source link

schema is not being ran when running docker-compose #558

Open JTan242 opened 6 months ago

JTan242 commented 6 months ago

I can't figure out why my schema isn't being read when I run docker-compose.

The error I am getting says that there is no relation to the table I am trying to insert data into my postgres database:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist
1379 LINE 2: INSERT INTO users (username, password) VALUES  
  ('VcdI...
1380^
1381
1382[SQL:
1383INSERT INTO users (username, password) VALUES (%(u)s, %(p)s);
1384]
1385
[parameters: {'u': 'VcdIS2oMIa', 'p': '9sxEljsmvw'}]

Is there something wrong with my docker-compose.yml or my schema that could be causing this problem?

My docker-compose.yml

version: '3.8'

services:
  web:
    build: ./services/web
    command: python manage.py run -h 0.0.0.0
    volumes:
      - ./services/web/:/usr/src/app/
    ports:
      - 2425:5000
    env_file:
      - ./.env.dev
    depends_on:
        - postgres
  postgres:
    build: services/postgres
    volumes:
      - ./:/tmp/db
      - postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=pass
      - PGUSER=postgres
    ports:
      - 2424:5432

volumes:
  postgres:

My schema:

SET maintenance_work_mem = '16GB';
SET max_parallel_maintenance_workers = 80;

CREATE TABLE urls (
    id_urls BIGSERIAL PRIMARY KEY,
    url TEXT UNIQUE
);

CREATE TABLE users (
    id_users BIGSERIAL PRIMARY KEY,
    username TEXT NOT NULL UNIQUE,
    password TEXT NOT NULL
);

CREATE TABLE tweets (
    id_tweets BIGSERIAL PRIMARY KEY,
    id_users INTEGER NOT NULL REFERENCES users(id_users) ON DELETE CASCADE,
    text TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
    id_urls INTEGER REFERENCES urls(id_urls)
);
mikeizbicki commented 6 months ago

There are many possible reasons why you could be getting this error message. The most likely problem, however, is that you've created a database with a different schema at some point. This database is in the volume, and so postgres does not overwrite the existing database when it is brought up and does not run your schema file. Deleting the volume will likely be a necessary (but possibly not sufficient) step for solving this problem.