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

Nullable ForeignKey field does not get updated to null #1229

Open
amit12297 opened this issue Nov 24, 2023 · 0 comments · May be fixed by #1230
Open

Nullable ForeignKey field does not get updated to null #1229

amit12297 opened this issue Nov 24, 2023 · 0 comments · May be fixed by #1230
Labels
bug Something isn't working

Comments

@amit12297
Copy link

Describe the bug
Nullable ForeignKey field does not get updated to null

To Reproduce
Steps to reproduce the behavior:

  1. Create 2 models, User and Vehicle, User model will have a vehicle foreignKey field which is nullable
class Vehicle(ormar.Model):
    class Meta(BaseMeta):
        tablename = "vehicles"

    id: int = ormar.Integer(primary_key=True)
    chasis_no: str = ormar.String(max_length=64, unique=True)
    registration_no: str = ormar.String(max_length=64, unique=True)
    model: str = ormar.String(max_length=128)


class User(ormar.Model):
    class Meta(BaseMeta):
        tablename = "users"

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=64, unique=True)
    vehicle: Vehicle = ormar.ForeignKey(to=Vehicle, nullable=True)
    email: str = ormar.String(nullable=True, max_length=100)
  1. Create a Vehicle (via an endpoint exposed)
@app.post("/vehicle")
async def add_vehicle():
    vehicle = await Vehicle.objects.create(
        chasis_no="12345",
        registration_no="56789",
        model="Hilux"
    )
    return vehicle

Screenshot 2023-11-24 at 11 43 11 PM

  1. Create User with vehicle from above step, User gets created fine with vehicle (via an endpoint exposed)
@app.post("/user")
async def add_user(request: AddUserRequest):
    vehicle = await Vehicle.objects.get_or_none(id=request.vehicle_id)
    user = await User.objects.create(
        name=request.name,
        vehicle=vehicle,
        email=request.email
    )
    return user

Screenshot 2023-11-24 at 11 44 35 PM

  1. Update User created above, set vehicle=None and email=None (via an endpoint exposed)
@app.post("/del_vehicle_email_from_user")
async def unassign_vehicle_from_user(request: UnassignVehicleRequest):
    user = await User.objects.get_or_none(id=request.user_id)
    user.vehicle = None
    user.email = None
    user = await user.update(_columns=["vehicle", "email"])
    return user
  1. Open Users table in database and check, Vehicle column which is a foreign-key wont be updated to null but email gets updated to null
    Screenshot 2023-11-24 at 11 46 07 PM

Full code of main.py

import uvicorn
from fastapi import FastAPI
from pydantic.main import BaseModel

from db import User, Vehicle, database

app = FastAPI()


@app.on_event("startup")
async def startup():
    if not database.is_connected:
        await database.connect()


@app.on_event("shutdown")
async def shutdown():
    if database.is_connected:
        await database.disconnect()


class AddUserRequest(BaseModel):
    vehicle_id: int
    name: str
    email: str


class UnassignVehicleRequest(BaseModel):
    user_id: int


@app.post("/vehicle")
async def add_vehicle():
    vehicle = await Vehicle.objects.create(
        chasis_no="12345",
        registration_no="56789",
        model="Hilux"
    )
    return vehicle


@app.post("/user")
async def add_user(request: AddUserRequest):
    vehicle = await Vehicle.objects.get_or_none(id=request.vehicle_id)
    user = await User.objects.create(
        name=request.name,
        vehicle=vehicle,
        email=request.email
    )
    return user


@app.post("/del_vehicle_email_from_user")
async def unassign_vehicle_from_user(request: UnassignVehicleRequest):
    user = await User.objects.get_or_none(id=request.user_id)
    user.vehicle = None
    user.email = None
    user = await user.update(_columns=["vehicle", "email"])
    return user


if __name__ == "__main__":
    uvicorn.run(app=app, port=8080, host="127.0.0.1", reload=False)

Expected behavior
Nullable foreignkey field should be set to null when updating

Screenshots
Added above

Versions (please complete the following information):

  • Database backend used - postgres
  • Python version - 3.11
  • ormar- 0.12.1
  • pydantic- 1.10.4
  • fastapi - 0.104.1

Additional context
Creating a User with vehicle=None works fine(Issue arises only while updating to None)

@amit12297 amit12297 added the bug Something isn't working label Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant