go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

goctl model pg "missing primary key"

Open duanmingze-coder opened this issue 4 months ago • 1 comments

user goctl model create code, return err

ddl:

create table if not exists order ( order_id int, transaction_id varchar(32), wallet_tx_id int, user_id int, round_id varchar(32), parent_round_id varchar(32), amount int, rtp smallint, status smallint, response smallint, settle_time int, created_at int, updated_at int, -- Table-level constraints constraint pk_order_id primary key (order_id, settle_time), constraint uk_transaction_id unique (transaction_id, settle_time) include (status,amount, user_id) ) partition by range (settle_time);

create index if not exists idx_round_time on order (round_id, settle_time); create index if not exists idx_parent_round_time on order (parent_round_id, settle_time);

goctl erro

db:public, table:bet_order, missing primary key

duanmingze-coder avatar Aug 12 '25 09:08 duanmingze-coder

Hi @duanmingze-coder,

Thank you for reporting this issue. After investigating your DDL, I can see that you're using a composite primary key:

constraint pk_order_id primary key (order_id, settle_time)

Unfortunately, go-zero's goctl model currently does not support composite (multi-column) primary keys. The model generator is designed to work with single-column primary keys only.

Current Limitation

The parser explicitly rejects tables with composite primary keys to avoid generating incorrect or incomplete CRUD code. This is why you're seeing the "missing primary key" error - the tool doesn't recognize composite keys as valid primary keys.

Workarounds

Here are some alternatives you can consider:

  1. Add a single-column primary key: Add an auto-increment id column as the primary key and convert your current composite key to a unique constraint:
create table if not exists order
(
    id               serial primary key,  -- New single-column PK
    order_id         int,
    settle_time      int,
    -- ... other columns
    constraint uk_order_settle unique (order_id, settle_time),  -- Convert to unique constraint
    constraint uk_transaction_id unique (transaction_id, settle_time)
        include (status, amount, user_id)
) partition by range (settle_time);
  1. Manual code generation: Use goctl model as a starting point with a temporary single-column primary key, then manually modify the generated code to handle your composite key requirements.

  2. Custom model implementation: Write your own model code without using goctl model for this specific table.

Future Support

Supporting composite primary keys would require significant changes to:

  • The DDL parser
  • Code generation templates
  • Generated CRUD methods
  • Cache key generation logic

This is a valid feature request, but it would be a substantial enhancement rather than a simple bug fix. If there's enough community interest, this could be considered for a future release.

For now, I recommend using workaround #1 (adding a single-column primary key) as it's the most compatible approach with go-zero's current architecture.

Hope this helps clarify the situation!

kevwan avatar Aug 16 '25 03:08 kevwan