SeaQL / SeaQL/sea-orm

Clarification on Relationship Generation in sea-orm-cli Based on ForeignKey Constraints

Open
#2,216 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category:question
Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Hello sea-orm team,

I am encountering a situation where the sea-orm-cli generate entity command generates different relationship annotations based on how the foreign key constraints are specified in the SQLite schema. Specifically, there seems to be a difference in behavior when the UNIQUE keyword is used.

Here's a detailed example using two tables, Authors and Books, to illustrate my point:

Schema Definition
CREATE TABLE Authors (
    ID INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

CREATE TABLE Books (
    ID INTEGER PRIMARY KEY AUTOINCREMENT,
    authorID INTEGER,
    title TEXT NOT NULL,
    FOREIGN KEY (authorID) REFERENCES Authors(ID)
);

CREATE UNIQUE INDEX idx_books_author ON Books (authorID);

With this schema, I expect that the relationship generated should indicate a one-to-one relationship (has_one) between Authors and Books due to the unique index on Books.authorID. However, the behavior seems inconsistent depending on the placement and usage of UNIQUE constraints directly in the table definition versus the creation of a unique index separately.

Could you please clarify the following:

How does sea-orm-cli generate entity determine when to generate #[sea_orm(has_one = "related_entity")] vs #[sea_orm(has_many = "related_entity")]?
Is there a recommended way of defining foreign keys and unique constraints in the schema to ensure consistent relationship generation?
Any guidance or clarification on this matter would be greatly appreciated.

Thank you for your assistance!

##Versions
├── sea-orm v0.12.14
│ ├── sea-orm-macros v0.12.14 (proc-macro)
│ │ ├── sea-bae v0.2.0 (proc-macro)
│ ├── sea-query v0.30.7
│ │ ├── sea-query-derive v0.4.1 (proc-macro)
│ ├── sea-query-binder v0.5.0
│ │ ├── sea-query v0.30.7 ()
├── sea-query v0.30.7 (
)
│ ├── sea-orm v0.12.14 ()
│ ├── sea-query v0.30.7 (
)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the sea-orm-cli generate entity entry point and reproduce the Authors/Books SQLite schema shown in the issue, comparing an inline UNIQUE constraint with the separate unique index. Trace how the foreign key metadata becomes has_one or has_many; done means the behavior is explained and any inconsistency is documented or corrected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql, sqlite
Domain
cli, databases, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.