Added foreign_key table constraint

This commit is contained in:
Noah Metz
2024-01-25 22:20:50 -07:00
parent 0fc8ec99fa
commit 15eaabebc8
2 changed files with 9 additions and 2 deletions

View File

@@ -31,7 +31,7 @@
-type column_constraint() :: non_null | primary_key | {primary_key, pk_constraints()}
| unique | {default, sql_value()}.
-type column_constraints() :: column_constraint() | [column_constraint()].
-type table_constraint() :: {primary_key, [atom()]} | {unique, [atom()]}.
-type table_constraint() :: {primary_key, [atom()]} | {foreign_key, {[atom()], atom(), [atom()]}} | {unique, [atom()]}.
-type table_constraints() :: table_constraint() | [table_constraint()].
-type table_info() :: [{column_id(), sql_type()} | {column_id(), sql_type(), column_constraints()}].

View File

@@ -376,7 +376,14 @@ table_constraint_sql(TableConstraint) ->
{unique, Columns} ->
["UNIQUE(",
map_intersperse(fun indexed_column_sql/1, Columns, ", "), ")"];
%% TODO: foreign key
{foreign_key, {Columns, Parent, ParentColumns}} ->
["FOREIGN KEY(",
map_intersperse(fun indexed_column_sql/1, Columns, ", "),
") REFERENCES ",
atom_to_list(Parent),
"(",
map_intersperse(fun indexed_column_sql/1, ParentColumns, ", "),
")"];
{raw, S} when is_list(S) -> S;
_ when is_list(TableConstraint) ->
map_intersperse(fun table_constraint_sql/1, TableConstraint, ", ")