1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use super::{ColumnTrait, IdenStatic, Iterable};
use crate::{TryFromU64, TryGetableMany};
use sea_query::{FromValueTuple, IntoValueTuple};
use std::fmt::Debug;

//LINT: composite primary key cannot auto increment
/// A Trait for to be used to define a Primary Key.
///
/// A primary key can be derived manually
///
/// ### Example
/// ```text
/// use sea_orm::entity::prelude::*;
///
/// #[derive(Copy, Clone, Debug, EnumIter)]
/// pub enum PrimaryKey {
///     Id,
/// }
/// impl PrimaryKeyTrait for PrimaryKey {
///     type ValueType = i32;
///
///     fn auto_increment() -> bool {
///         true
///     }
/// }
/// ```
///
/// Alternatively, use derive macros to automatically implement the trait for a Primary Key
///
/// ### Example
/// ```text
/// use sea_orm::entity::prelude::*;
///
/// #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
/// pub enum PrimaryKey {
///     Id,
/// }
/// ```
/// See module level docs [crate::entity] for a full example
pub trait PrimaryKeyTrait: IdenStatic + Iterable {
    #[allow(missing_docs)]
    type ValueType: Sized
        + Send
        + Debug
        + PartialEq
        + IntoValueTuple
        + FromValueTuple
        + TryGetableMany
        + TryFromU64;

    /// Method to call to perform `AUTOINCREMENT` operation on a Primary Kay
    fn auto_increment() -> bool;
}

/// How to map a Primary Key to a column
pub trait PrimaryKeyToColumn {
    #[allow(missing_docs)]
    type Column: ColumnTrait;

    /// Method to map a primary key to a column in an Entity
    fn into_column(self) -> Self::Column;

    /// Method to map a primary key from a column in an Entity
    fn from_column(col: Self::Column) -> Option<Self>
    where
        Self: Sized;
}

#[cfg(test)]
mod tests {
    #[test]
    #[cfg(feature = "macros")]
    fn test_composite_primary_key() {
        mod primary_key_of_1 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_1")]
            pub struct Model {
                #[sea_orm(primary_key)]
                pub id: i32,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }

        mod primary_key_of_2 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_2")]
            pub struct Model {
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_1: i32,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_2: String,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }

        mod primary_key_of_3 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_3")]
            pub struct Model {
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_1: i32,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_2: String,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_3: Uuid,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }

        mod primary_key_of_4 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_4")]
            pub struct Model {
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_1: TimeDateTimeWithTimeZone,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_2: Uuid,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_3: Json,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_4: Decimal,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }

        mod primary_key_of_11 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
            #[sea_orm(
                rs_type = "String",
                db_type = "String(Some(1))",
                enum_name = "category"
            )]
            pub enum DeriveCategory {
                #[sea_orm(string_value = "B")]
                Big,
                #[sea_orm(string_value = "S")]
                Small,
            }

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_11")]
            pub struct Model {
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_1: Vec<u8>,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_2: DeriveCategory,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_3: Date,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_4: DateTime,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_5: Time,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_6: TimeTime,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_7: DateTime,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_8: TimeDateTime,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_9: DateTimeLocal,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_10: DateTimeUtc,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_11: DateTimeWithTimeZone,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }

        mod primary_key_of_12 {
            use crate as sea_orm;
            use crate::entity::prelude::*;

            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = "primary_key_of_12")]
            pub struct Model {
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_1: String,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_2: i8,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_3: u8,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_4: i16,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_5: u16,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_6: i32,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_7: u32,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_8: i64,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_9: u64,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_10: f32,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_11: f64,
                #[sea_orm(primary_key, auto_increment = false)]
                pub id_12: bool,
                pub owner: String,
                pub name: String,
                pub description: String,
            }

            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}

            impl ActiveModelBehavior for ActiveModel {}
        }
    }
}