How to extend a model with inherit=
Goal: add stored fields (and matching view xpath) to an existing ORM model from a dependent module without duplicating the base struct.
When to use
- Your module
dependson the module that owns the model (e.g. contacts depends on base forcore.partner). - You need new columns on the same PostgreSQL table, not a separate model.
Steps
- Add an extension struct in your addon
models/package:
```go title="models/partner_extend.go" copy lines
package models
import "sumeru/core/sdk"
type PartnerContacts struct {
sdk.Model sumeru:"inherit=core.partner"
CustomerRank sdk.Integer sumeru:"string=Customer Rank,default=0"
}
```
Use inherit= with the technical model name of the target. Do not set model= on the same struct — the tags are mutually exclusive.
- Blank-import models from addon
init.goif not already:
```go title="init.go" copy
import _ "sumeru/addons/contacts/models"
```
- Run
make generate(ormake build) somodels/zmodels.gocallssdk.MustRegisterfor the extension struct.
- Update schema by installing or updating your module:
```bash shell copy
go run ./cmd/sumeru -- -c sumeru.conf -u contacts --stop-after-init
```
SyncRegistrySchemaForModule includes models extended by the module and runs additive DDL for new columns.
- Expose fields in the UI with view xpath inherit (see View inherit xpath).
Go + XML together (contacts example)
package models import "sumeru/core/sdk" type PartnerContacts struct { sdk.Model `sumeru:"inherit=core.partner"` CustomerRank sdk.Integer `sumeru:"string=Customer Rank,default=0"`}<record id="view_core_partner_form_contacts_inherit" model="sys.view"> <field name="inherit_id" ref="contacts.view_core_partner_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='phone']" position="after"> <field name="customer_rank" string="Customer Rank"/> </xpath> </field></record>Rules and limits
- The base model must register first (enforce with
manifest.dependsand correct addon import order). - Duplicate field names on the same model cause a startup panic with a clear error.
- Go method override on models is not supported in v1 — use
RegisterObjectActionorevent.Subscribefor behavior. - Delegation inherit (parent table per record) is out of scope for this mechanism.
Live example
- Go:
sumeru/addons/contacts/models/partner_extend.go - View:
sumeru/addons/contacts/views/core_partner_form_inherit_views.xml