Skip to content

Commit

Permalink
feat(schema-engine): use pk for implicit join tables on postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Nov 26, 2024
1 parent 06d4b2f commit ff2365f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,24 @@ fn push_relation_tables(ctx: &mut Context<'_>) {
},
);

// Unique index on AB
// Unique index or PK on AB
{
let index_name = format!(
"{}_AB_unique",
table_name.chars().take(max_identifier_length - 10).collect::<String>()
);
let index_id = ctx.schema.describer_schema.push_unique_constraint(table_id, index_name);
let index_id = if ctx.flavour.uses_pk_in_m2m_join_table() {
let constraint_name = format!(
"{}_AB_pk",
table_name.chars().take(max_identifier_length - 6).collect::<String>()
);

ctx.schema.describer_schema.push_primary_key(table_id, constraint_name)
} else {
let index_name = format!(
"{}_AB_unique",
table_name.chars().take(max_identifier_length - 10).collect::<String>()
);

ctx.schema.describer_schema.push_unique_constraint(table_id, index_name)
};

ctx.schema.describer_schema.push_index_column(sql::IndexColumn {
index_id,
column_id: column_a_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ pub(crate) trait SqlSchemaCalculatorFlavour {
}

fn push_connector_data(&self, _context: &mut super::Context<'_>) {}

// TODO: this should be an enum
fn uses_pk_in_m2m_join_table(&self) -> bool {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl SqlSchemaCalculatorFlavour for PostgresFlavour {
.describer_schema
.set_connector_data(Box::new(postgres_ext));
}

fn uses_pk_in_m2m_join_table(&self) -> bool {
true
}
}

fn convert_opclass(opclass: OperatorClass, algo: Option<IndexAlgorithm>) -> sql::postgres::SQLOperatorClass {
Expand Down

0 comments on commit ff2365f

Please sign in to comment.