Computed foreign keys

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

You may want to create a relationship between 2 Collections, but you don't have a foreign key that is ready to use to connect them.

To solve that use case, you should use both computed fields and relationships.

This is done with the following steps:

  • create a new Field containing a foreign key,

  • make the Field filterable for the In operator (see Under the hood as to why this is required),

  • create a relation using it.

We have 2 Collections: Customers and Messages, linked together by a one-to-many relationship.

We want to create a ManyToOne relationship with the last message sent by a given customer.

from forestadmin.datasource_toolkit.context.collection_context import (
    CollectionCustomizationContext,
)
from forestadmin.datasource_toolkit.interfaces.fields import Operator
from forestadmin.datasource_toolkit.interfaces.query.aggregation import (
    Aggregation,
    Aggregator,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.base import (
    ConditionTree,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import (
    ConditionTreeLeaf,
)
from forestadmin.datasource_toolkit.interfaces.query.filter.unpaginated import Filter
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias

async def get_last_message_id(
    customers: RecordsDataAlias, context: CollectionCustomizationContext
):
    # We're using Forest Admin's Query Interface (you can use an ORM or plain SQL)
    messages = context.datasource.get_collection("messages")
    condition_tree = ConditionTreeLeaf(
        "customer_id", Operator.IN, [c["id"] for c in customers]
    )

    rows = await messages.aggregate(
        context.caller,
        Filter({"condition_tree": condition_tree}),
        Aggregation(
            {
                "field": "id",
                "operation": "Max",
                "groups": [{"field": "customer_id"}],
            }
        ),
    )
    ret = []
    for customer in customers:
        row = filter(lambda r: r["group"]["customer_id"] == customer["id"], rows)
        row = row[0] if len(row) == 1 else {}
        ret.append(row.get("value"))
    return ret

async def last_message_in_operator(
    last_message_ids: List[Any], context: CollectionCustomizationContext
) -> ConditionTree:
    records = await context.datasource.get_collection("messages").list(
        context.caller,
        Filter(
            {
                "condition_tree": ConditionTreeLeaf(
                    "id", Operator.IN, last_message_ids
                )
            }
        ),
        ["customer_id"],
    )
    return ConditionTreeLeaf("id", Operator.IN, [r["customer_id"] for r in records])

agent.customize_collection("Customers").add_field(
    "lastMessageId",
    {
        # Create foreign key
        "column_type": "Number",
        "dependencies": ["id"],
        "get_values": get_last_message_id,
    },
).replace_field_operator(
    # Implement the 'In' operator.
    "lastMessageId",
    Operator.IN,
    last_message_in_operator,
).add_many_to_one_relation(
    # Create relationships using the foreign key we just added.
    name="lastMessage",
    foreign_collection="messages",
    foreign_key="lastMessageId",
)

Connecting collections without having a shared identifier

You have 2 Collections and both contain users: one comes from your database, and the other one is connected to the CRM that your company uses.

There is no common id between them that can be used to tell Forest Admin how to link them together, however, both Collections have firstName, lastName, and birthDate fields, which taken together, are unique enough.

from forestadmin.datasource_toolkit.datasource_customizer.collection_customizer import (
    CollectionCustomizer,
)
from forestadmin.datasource_toolkit.interfaces.fields import Operator
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.branch import (
    Aggregator,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.branch import (
    ConditionTreeBranch,
)
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import (
    ConditionTreeLeaf,
)

# Concatenate firstname, lastname and birthData to make a unique identifier
# and ensure that the new field is filterable
def create_filterable_identity_field(collection: CollectionCustomizer):
    # Create foreign key on the collection from the database
    collection.add_field(
        "userIdentifier",
        {
            "column_type": "String",
            "dependencies": ["firstName", "lastName", "birthDate"],
            "get_values": lambda users, context: [
                f"{u['firstName']}/{u['lastName']}/{u['birthDate']}" for u in users
            ],
        },
    )

    # Implement 'In' filtering operator (required)
    collection.replace_field_operator(
        "userIdentifier",
        Operator.IN,
        lambda values, ctx: ConditionTreeBranch(
            Aggregator.OR,
            [
                ConditionTreeBranch(
                    Aggregator.AND,
                    [
                        ConditionTreeLeaf(
                            "firstName", Operator.EQUAL, value.split("/")[0]
                        ),
                        ConditionTreeLeaf(
                            "lastName", Operator.EQUAL, value.split("/")[1]
                        ),
                        ConditionTreeLeaf(
                            "birthDate", Operator.EQUAL, value.split("/")[2]
                        ),
                    ],
                )
                for value in values
            ],
        ),
    )

create_filterable_identity_field(agent.customize_collection("databaseUsers"))
create_filterable_identity_field(agent.customize_collection("crmUsers"))

# Create relationship between databaseUsers and crmUsers
agent.customize_collection("databaseUsers").add_one_to_one_relation(
    name="userFromCrm",
    foreign_collection="crmUsers",
    origin_key="userIdentifier",
    origin_key_target="userIdentifier",
)
# Create relationship between crmUsers and databaseUsers
agent.customize_collection("databaseUsers").add_many_to_one_relation(
    name="userFromCrm",
    foreign_collection="crmUsers",
    foreign_key="userIdentifier",
    foreign_key_target="userIdentifier",
)

Last updated