Defining Relations — Aquilia Documentation
Comprehensive guide and documentation for Defining Relations in the Aquilia framework. View API reference, examples, and implementation patterns.
Docs / Models / Defining Relationships Defining Relationships Aquilia ORM supports first-class relational fields including Many-to-One, One-to-One, and Many-to-Many configurations. ForeignKey (Many-to-One) Declares a many-to-one relationship. Requires the target model (either class reference or forward reference string) and on_delete behavior: from aquilia.models.fields_module import ForeignKey class Post(Model): # Class reference author = ForeignKey(User, on_delete="CASCADE", related_name="posts") # Or string reference (prevents circular imports) category = ForeignKey("Category", on_delete="SET_NULL", null=True) On-Delete Actions Supported database-level delete cascades: "CASCADE": Cascades the deletion of the referenced row to this row. "SET_NULL": Sets the foreign key column to NULL (requires null=True). "RESTRICT": Rejects parent deletion if dependent children rows exist. "SET_DEFAULT": Sets the column to its configured default value. "DO_NOTHING": No database-level action is taken (raw foreign key remains unchanged). OneToOneField Similar to ForeignKey, but enforces a UNIQUE constraint on the foreign key column, establishing a strict 1-to-1 link: from aquilia.models.fields_module import OneToOneField class Profile(Model): user = OneToOneField(User, on_delete="CASCADE", related_name="profile") ManyToManyField Configures a many-to-many relationship. Automatically generates an intermediary junction table: from aquilia.models.fields_module import ManyToManyField class Article(Model): tags = ManyToManyField("Tag", related_name="articles") QuerySet API Hydration Primitives )
Go to Homepage