sea_orm/entity/mod.rs
1//! Entities and the types and traits that describe them.
2//!
3//! In SeaORM, every database table is represented by an **Entity** — a unit
4//! struct implementing [`EntityTrait`]. The Entity ties together:
5//!
6//! - the table's **name** (via [`EntityName`]),
7//! - its **columns** (via [`ColumnTrait`] and the strongly-typed `COLUMN` constant),
8//! - its **primary key** (via [`PrimaryKeyTrait`] / [`PrimaryKeyToColumn`]),
9//! - its **relations** to other entities (via [`RelationTrait`]; in 2.0,
10//! relations can also be declared directly on the `Model` struct as
11//! [`HasOne`](compound::HasOne) / [`HasMany`](compound::HasMany) fields).
12//!
13//! Each Entity has two companion types:
14//!
15//! - **[`Model`](ModelTrait)** — a plain struct mirroring a row of the table,
16//! used for reads.
17//! - **[`ActiveModel`](ActiveModelTrait)** — a struct where every field is
18//! wrapped in [`ActiveValue`], used for inserts and partial updates.
19//!
20//! From an Entity you can build select, insert, update, and delete queries
21//! via [`EntityTrait::find`], [`EntityTrait::insert`], [`EntityTrait::update`],
22//! and [`EntityTrait::delete`] (plus their `_many` / `_by_id` siblings).
23//!
24//! # Defining an Entity (2.0 dense format)
25//!
26//! The recommended way to define an entity in SeaORM 2.0 is the dense entity
27//! format, where the relations live directly on the `Model` struct as
28//! [`HasOne`](compound::HasOne) / [`HasMany`](compound::HasMany) fields:
29//!
30//! ```
31//! # #[cfg(feature = "macros")]
32//! # mod entities {
33//! # mod fruit {
34//! # use sea_orm::entity::prelude::*;
35//! # #[sea_orm::model]
36//! # #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
37//! # #[sea_orm(table_name = "fruit")]
38//! # pub struct Model {
39//! # #[sea_orm(primary_key)]
40//! # pub id: i32,
41//! # pub cake_id: Option<i32>,
42//! # #[sea_orm(belongs_to, from = "cake_id", to = "id")]
43//! # pub cake: HasOne<super::cake::Entity>,
44//! # }
45//! # impl ActiveModelBehavior for ActiveModel {}
46//! # }
47//! mod cake {
48//! use sea_orm::entity::prelude::*;
49//!
50//! #[sea_orm::model]
51//! #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
52//! #[sea_orm(table_name = "cake")]
53//! pub struct Model {
54//! #[sea_orm(primary_key)]
55//! pub id: i32,
56//! pub name: String,
57//! #[sea_orm(has_many)]
58//! pub fruits: HasMany<super::fruit::Entity>,
59//! }
60//!
61//! impl ActiveModelBehavior for ActiveModel {}
62//! }
63//! # }
64//! ```
65//!
66//! # Defining an Entity (1.0 compact format)
67//!
68//! The 1.0 compact format remains supported. Here relations are declared as a
69//! separate `Relation` enum implementing [`RelationTrait`], plus an explicit
70//! [`Related`] impl per foreign entity:
71//!
72//! ```
73//! # #[cfg(feature = "macros")]
74//! # mod entities {
75//! # mod fruit {
76//! # use sea_orm::entity::prelude::*;
77//! # #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
78//! # #[sea_orm(table_name = "fruit")]
79//! # pub struct Model {
80//! # #[sea_orm(primary_key)]
81//! # pub id: i32,
82//! # pub cake_id: Option<i32>,
83//! # }
84//! # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
85//! # pub enum Relation {
86//! # #[sea_orm(
87//! # belongs_to = "super::cake::Entity",
88//! # from = "Column::CakeId",
89//! # to = "super::cake::Column::Id"
90//! # )]
91//! # Cake,
92//! # }
93//! # impl Related<super::cake::Entity> for Entity {
94//! # fn to() -> RelationDef { Relation::Cake.def() }
95//! # }
96//! # impl ActiveModelBehavior for ActiveModel {}
97//! # }
98//! mod cake {
99//! use sea_orm::entity::prelude::*;
100//!
101//! #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
102//! #[sea_orm(table_name = "cake")]
103//! pub struct Model {
104//! #[sea_orm(primary_key)]
105//! pub id: i32,
106//! pub name: String,
107//! }
108//!
109//! #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
110//! pub enum Relation {
111//! #[sea_orm(has_many = "super::fruit::Entity")]
112//! Fruit,
113//! }
114//!
115//! impl Related<super::fruit::Entity> for Entity {
116//! fn to() -> RelationDef {
117//! Relation::Fruit.def()
118//! }
119//! }
120//!
121//! impl ActiveModelBehavior for ActiveModel {}
122//! }
123//! # }
124//! ```
125//!
126//! Entity files are usually generated for you with `sea-orm-cli` against an
127//! existing database. See the [crate-level documentation](crate) for a
128//! walkthrough of the most common operations.
129mod active_enum;
130mod active_model;
131mod active_model_ex;
132mod active_value;
133#[cfg(feature = "with-arrow")]
134mod arrow_schema;
135mod base_entity;
136pub(crate) mod column;
137mod column_def;
138pub mod compound;
139mod identity;
140mod link;
141mod model;
142mod partial_model;
143/// Re-exports the types and traits most commonly needed to define and use
144/// entities. Glob-import this module in entity files: `use sea_orm::entity::prelude::*;`.
145pub mod prelude;
146mod primary_key;
147#[cfg(feature = "entity-registry")]
148mod registry;
149mod relation;
150#[cfg(feature = "with-arrow")]
151pub(crate) mod with_arrow;
152
153pub use active_enum::*;
154pub use active_model::*;
155pub use active_model_ex::*;
156pub use active_value::*;
157#[cfg(feature = "with-arrow")]
158pub use arrow_schema::*;
159pub use base_entity::*;
160pub use column::*;
161pub use column_def::*;
162pub use compound::EntityLoaderTrait;
163pub use identity::*;
164pub use link::*;
165pub use model::*;
166pub use partial_model::*;
167pub use primary_key::*;
168#[cfg(feature = "entity-registry")]
169pub use registry::*;
170pub use relation::*;