| 154 | } |
| 155 | |
| 156 | pub(crate) fn create_table_from_entity<E>(entity: E, backend: DbBackend) -> TableCreateStatement |
| 157 | where |
| 158 | E: EntityTrait, |
| 159 | { |
| 160 | let mut stmt = TableCreateStatement::new(); |
| 161 | |
| 162 | if let Some(comment) = entity.comment() { |
| 163 | stmt.comment(comment); |
| 164 | } |
| 165 | |
| 166 | for column in E::Column::iter() { |
| 167 | let mut column_def = column_def_from_entity_column::<E>(column, backend); |
| 168 | stmt.col(&mut column_def); |
| 169 | } |
| 170 | |
| 171 | if <<E::PrimaryKey as PrimaryKeyTrait>::ValueType as PrimaryKeyArity>::ARITY > 1 { |
| 172 | let mut idx_pk = Index::create(); |
| 173 | for primary_key in E::PrimaryKey::iter() { |
| 174 | idx_pk.col(primary_key); |
| 175 | } |
| 176 | stmt.primary_key(idx_pk.name(format!("pk-{}", entity.to_string())).primary()); |
| 177 | } |
| 178 | |
| 179 | for relation in E::Relation::iter() { |
| 180 | let relation = relation.def(); |
| 181 | if relation.is_owner { |
| 182 | continue; |
| 183 | } |
| 184 | stmt.foreign_key(&mut relation.into()); |
| 185 | } |
| 186 | |
| 187 | stmt.table(entity.table_ref()).take() |
| 188 | } |
| 189 | |
| 190 | fn column_def_from_entity_column<E>(column: E::Column, backend: DbBackend) -> ColumnDef |
| 191 | where |