We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Describe the bug Nullable ForeignKey field does not get updated to null
To Reproduce Steps to reproduce the behavior:
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)
@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
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):
ormar
pydantic
fastapi
Additional context Creating a User with vehicle=None works fine(Issue arises only while updating to None)
The text was updated successfully, but these errors were encountered:
+1 when I set a related field to null, it becomes some strange zombie object with None fore every field but id and pk.
Sorry, something went wrong.
Successfully merging a pull request may close this issue.
Describe the bug
Nullable ForeignKey field does not get updated to null
To Reproduce
Steps to reproduce the behavior:
Full code of main.py
Expected behavior
Nullable foreignkey field should be set to null when updating
Screenshots
Added above
Versions (please complete the following information):
ormar
- 0.12.1pydantic
- 1.10.4fastapi
- 0.104.1Additional context
Creating a User with vehicle=None works fine(Issue arises only while updating to None)
The text was updated successfully, but these errors were encountered: