#1048·ent

Will polymorphism be available?

Author: satriahrhCreated Dec 15, 2020Updated May 13, 2026
LabelsProposal
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary

I first used polymorphism in ActiveRecord, it is called Polymorphic Associations.

Here is how I expected this should work:

For example, a retail company have two types of Transaction: InboundTransaction (add stock, procurement, expense, etc) and OutboundTransaction (client order, end user order, etc). But it is have one in common, amount to track down the budget. It will be easier for the accountant to keep track on them if it is placed as single table, just like how mutation works. Inbound and outbound transaction has its own behaviour, will be troublesome if we merged them into one.

OutboundTransaction
go
type OutboundTransaction struct {
  ent.Schema
}

func (OutboundTransaction) Fields() []ent.Field {
  return []ent.Field{
    fie.d.String("sales_channel"),
    field.JSON("products_dealed", ProductDealed{}),
    field.String("courier"),
    field.Strign("courier_tracking_number")
  }
}
InboundTransaction
go
type InboundTransaction struct {
  ent.Schema
}

func (OutboundTransaction) Fields() []ent.Field {
  return []ent.Field{
    field.String("department_in_charge"),
    field.String("procurement_document_url"),
    field.Time("arival"),
  }
}
Transaction
go
type Transaction struct {
  ent.Schema
}

func (Transaction) Fields() []ent.Field {
  return []ent.Field{
    field.Int("amount"),
  }
}

func (Transaction) IsA() []ent.IsA {
  return []ent.IsA{
    isA.To(OutboundTransaction.Type),
    isA.To(InboundTransaction.Type),
  }
}

Motivation

I found it pretty interesting when you have several nodes within common structure but have another specific different thing. Those different fields could be the edges or fields, make it to hard if we keep them intake but at the same time make it to hard not to take them out.