{"id":24790118,"url":"https://github.com/datamonet/flask-db","last_synced_at":"2025-06-17T21:44:47.886Z","repository":{"id":273903118,"uuid":"921254600","full_name":"datamonet/flask-db","owner":"datamonet","description":"Flask-SQLAlchemy + Alembic Tutorial","archived":false,"fork":false,"pushed_at":"2025-01-23T16:38:56.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T17:28:50.586Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/datamonet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-23T16:16:37.000Z","updated_at":"2025-01-23T16:38:59.000Z","dependencies_parsed_at":"2025-01-23T17:29:17.453Z","dependency_job_id":"7495ea08-c840-4fe3-aa91-a9a3fe580014","html_url":"https://github.com/datamonet/flask-db","commit_stats":null,"previous_names":["datamonet/flask-db"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datamonet%2Fflask-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datamonet%2Fflask-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datamonet%2Fflask-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datamonet%2Fflask-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/datamonet","download_url":"https://codeload.github.com/datamonet/flask-db/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245276180,"owners_count":20588894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-29T18:14:45.532Z","updated_at":"2025-03-24T13:14:12.256Z","avatar_url":"https://github.com/datamonet.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask SQLite Student Database\n\nA simple Flask web application that uses SQLAlchemy to manage student information in a SQLite database.\n\n## Setup and Installation\n\n1. Create a virtual environment (optional but recommended):\n```bash\npython -m venv venv\nsource venv/bin/activate  # On Windows use: venv\\Scripts\\activate\n```\n\n2. Install required packages:\n```bash\npip install -r requirements.txt\n```\n\n3. Seed the database with sample data (optional):\n```bash\npython seed_db.py\n```\n\n4. Run the application:\n```bash\npython app.py\n```\n\n5. Open your web browser and visit: http://127.0.0.1:5000\n\n## Database\n\nThe application uses SQLite database (students.db) which will be automatically created when you run the application for the first time. The database contains a single table 'student' with the following fields:\n- id (Primary Key)\n- first_name\n- last_name\n- email (unique, nullable)\n\n## Sample Data\nThe seed script (`seed_db.py`) will populate the database with 10 sample students. You can run this script once to initialize the database with sample data. The script includes a check to prevent duplicate seeding.\n\n## Database Migrations\n\nThis project uses Alembic for database migrations. Here's how to work with migrations:\n\n1. Initialize migrations (already done):\n```bash\nalembic init migrations\n```\n\n2. Create a new migration:\n```bash\nalembic revision --autogenerate -m \"Description of the change\"\n```\n\n3. Apply migrations:\n```bash\nalembic upgrade head\n```\n\n4. Rollback migrations:\n```bash\nalembic downgrade -1  # Rollback one step\nalembic downgrade base  # Rollback all migrations\n```\n\n5. View migration history:\n```bash\nalembic history\n```\n\nNote: When making changes to the database schema, always use migrations rather than modifying the models directly. This ensures that your database changes are tracked and can be rolled back if needed.\n\n## How to Add a Phone Number Column\n\nFollow these steps to add a phone number column to the students table:\n\n1. Update the Student model in `app.py`:\n```python\nclass Student(db.Model):\n    # ... existing columns ...\n    phone_number = db.Column(db.String(20), nullable=True)\n```\n\n2. Create a new migration:\n```bash\nalembic revision --autogenerate -m \"Add phone number to Student model\"\n```\n\n3. Review the generated migration file in `migrations/versions/` to ensure it uses batch operations for SQLite:\n```python\ndef upgrade() -\u003e None:\n    with op.batch_alter_table('student', schema=None) as batch_op:\n        batch_op.add_column(sa.Column('phone_number', sa.String(length=20), nullable=True))\n\ndef downgrade() -\u003e None:\n    with op.batch_alter_table('student', schema=None) as batch_op:\n        batch_op.drop_column('phone_number')\n```\n\n4. Apply the migration:\n```bash\nalembic upgrade head\n```\n\n5. Update the `seed_db.py` script to include phone numbers:\n```python\nstudents = [\n    {\n        'first_name': 'John',\n        'last_name': 'Doe',\n        'email': 'john.doe@example.com',\n        'phone_number': '123-456-7890'\n    },\n    # ... add phone numbers for other students ...\n]\n```\n\n6. Update the template in `templates/index.html` to display phone numbers:\n```html\n\u003ctable\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n            \u003c!-- ... existing columns ... --\u003e\n            \u003cth\u003ePhone Number\u003c/th\u003e\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n        {% for student in students %}\n        \u003ctr\u003e\n            \u003c!-- ... existing columns ... --\u003e\n            \u003ctd\u003e{{ student.phone_number }}\u003c/td\u003e\n        \u003c/tr\u003e\n        {% endfor %}\n    \u003c/tbody\u003e\n\u003c/table\u003e\n```\n\n7. Reseed the database:\n```bash\npython seed_db.py\n```\n\n8. Restart the Flask application and verify the changes at http://127.0.0.1:5000\n\nNote: Make sure to follow these steps in order. The migration system will handle the database schema update without losing existing data. If you need to start fresh, you can use the seed script which will recreate the database with the new schema.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatamonet%2Fflask-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatamonet%2Fflask-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatamonet%2Fflask-db/lists"}