> ## Documentation Index
> Fetch the complete documentation index at: https://fastapi.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# CRUD Operations

> Perform basic Create, Read, Update, and Delete operations using SQLAlchemy ORM.

# CRUD Operations

All database operations are performed using a **Session**.

## Create a Session

### File: `database.py`

```python theme={null}
from sqlalchemy.orm import sessionmaker

SessionLocal = sessionmaker(bind=engine)
```

### File: `main.py`

```python theme={null}
from database import SessionLocal

session = SessionLocal()
```

## Create (Insert)

### File: `main.py`

```python theme={null}
student = Student(name="John", age=20)

session.add(student)

session.commit()
```

### Explanation

```python theme={null}
student = Student(name="John", age=20)
```

Creates a new `Student` object.

```python theme={null}
session.add(student)
```

Adds the object to the Session.

```python theme={null}
session.commit()
```

Saves the object to the database.

## Read (Select)

### File: `main.py`

```python theme={null}
from sqlalchemy import select

stmt = select(Student)

result = session.execute(stmt)

students = result.scalars().all()
```

### Explanation

```python theme={null}
stmt = select(Student)
```

Creates a query to retrieve all students.

```python theme={null}
result = session.execute(stmt)
```

Executes the query and returns a `Result` object.

```python theme={null}
students = result.scalars().all()
```

Extracts the `Student` objects and returns them as a Python list.

## Update

### File: `main.py`

```python theme={null}
student = session.get(Student, 1)

student.name = "John Doe"
student.age = 21

session.commit()
```

### Explanation

```python theme={null}
student = session.get(Student, 1)
```

Retrieves the student whose primary key is `1`.

```python theme={null}
student.name = "John Doe"
student.age = 21
```

Updates the object's attributes.

```python theme={null}
session.commit()
```

Saves the changes to the database.

### Alternative (Direct Query)

```python theme={null}
from sqlalchemy import update

stmt = (
    update(Student)
    .where(Student.id == 1)
    .values(name="John Doe", age=21)
)

session.execute(stmt)

session.commit()
```

## Delete

### File: `main.py`

```python theme={null}
student = session.get(Student, 1)

session.delete(student)

session.commit()
```

### Explanation

```python theme={null}
student = session.get(Student, 1)
```

Retrieves the student to be deleted.

```python theme={null}
session.delete(student)
```

Marks the object for deletion.

```python theme={null}
session.commit()
```

Removes the record from the database.

### Alternative (Direct Query)

```python theme={null}
from sqlalchemy import delete

stmt = delete(Student).where(Student.id == 1)

session.execute(stmt)

session.commit()
```

## Rollback Changes

### File: `main.py`

```python theme={null}
try:
    session.commit()
except Exception:
    session.rollback()
```

### Explanation

```python theme={null}
session.rollback()
```

Discards all uncommitted changes if an error occurs.

## Close the Session

### File: `main.py`

```python theme={null}
session.close()
```

### Explanation

```python theme={null}
session.close()
```

Closes the Session and releases the database connection.

## Quick Revision

| Operation | ORM Approach                                 | Direct Query                          |
| --------- | -------------------------------------------- | ------------------------------------- |
| Create    | `add()`                                      | —                                     |
| Read      | `select()` → `execute()` → `scalars().all()` | —                                     |
| Update    | `get()` → Modify → `commit()`                | `update()` → `execute()` → `commit()` |
| Delete    | `get()` → `delete()` → `commit()`            | `delete()` → `execute()` → `commit()` |
| Rollback  | `rollback()`                                 | —                                     |
| Close     | `close()`                                    | —                                     |

> **Note:** The object-based approach is the recommended way to work with SQLAlchemy ORM. Direct `update()` and `delete()` statements are useful for bulk operations and will be covered in detail in the **Advanced Queries** chapter.
