subsquid / squid-sdk

The main repo of the squid SDK
Apache License 2.0
1.23k stars 151 forks source link

PrimaryColumns other than 'id' are not supported in codegen #259

Open jeffrey-computers opened 7 months ago

jeffrey-computers commented 7 months ago

Describe the bug TypeORM @PrimaryColumn supports fields with an arbitrary name, but Squid's codegen only supports a PrimaryColumn named 'id'. If an 'id' value is not included in the schema, then it is added automatically.

This means code comments are required to explain what the entity's id value is, rather than semantic field names. As is evidenced in your own example project!

To Reproduce Create a schema.graphql file like:

type TestEntity @entity {
  someUniqueIdentifier: ID!
  foo: boolean;
}

Codegen automatically creates a @PrimaryColumn_() of id. The intended primary column is someUniqueIdentifier

Expected behavior

i.e. the above example Should yield:

@Entity_()
export class TestEntity {
    constructor(props?: Partial<TestEntity>) {
        Object.assign(this, props)
    }

    @PrimaryColumn_()
    someUniqueIdentifier!: string

    @Column_("bool", {nullable: true})
    foo!: boolean | undefined | null
}
rmcmk commented 4 months ago

I agree. Primary keys should not be restricted in this manner. Additionally, supporting composite primary keys would allow for more flexible indexing paradigms.

However, this may not be a trivial change. The code generator adheres to the OpenCRUD spec, which currently only supports primary keys of type ID (essentially a string) and does not support composite primary keys (maybe it does, am not completely familiar with the spec)