ForeignKeyColumn Class
This class is utilized when informing dataloom that a column has a relationship with a primary key in another table.
Consider the following model definition of a Post
class Post(Model):
__tablename__: Optional[TableColumn] = TableColumn(name="posts")
id = PrimaryKeyColumn(type="int", auto_increment=True, nullable=False, unique=True)
completed = Column(type="boolean", default=False)
title = Column(type="varchar", length=255, nullable=False)
# timestamps
createdAt = CreatedAtColumn()
updatedAt = UpdatedAtColumn()
# relations
userId = ForeignKeyColumn(
User, type="int", required=True, onDelete="CASCADE", onUpdate="CASCADE"
)
userIdis a foreign key in the tableposts, indicating it has a relationship with a primary key in theuserstable.
This column accepts the following arguments
Argument |
Description |
Type |
Default |
|
|
|
– |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tip
It is crucial to specify the actions for onDelete and onUpdate to ensure that dataloom manages your model’s relationship actions appropriately. The available actions are
"NO ACTION"- If you delete or update the parent table, no changes will occur in the child table."SET NULL"- If you delete or update the parent table, the corresponding value in the child table will be set tonull."CASCADE"- If you delete or update the table, the same action will also be applied to the child table.