Class: PgMeta::Table
- Inherits:
-
Object
- Object
- PgMeta::Table
- Defined in:
- lib/pg_graph/ext/meta.rb
Instance Method Summary collapse
-
#mm_table? ⇒ Boolean
True if table is a N:M link table.
-
#nm_table? ⇒ Boolean
True if table is a N:N link table.
- #path ⇒ Object
Instance Method Details
#mm_table? ⇒ Boolean
True if table is a N:M link table. A N:M relation allows for multiple relations between two records
A table is a N:M table if
o it has a primary key named 'id' as the first column
o has two reference fields using the link field naming convention
o and nothing else
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pg_graph/ext/meta.rb', line 15 def mm_table? @mm_table ||= if columns.size != 3 false elsif columns.values.first.name != "id" || columns.values.first != primary_key_column false else referential_constraints.size == 2 && referential_constraints.values.map { |constraint| constraint.referencing_columns }.sort == [[columns.values[1]], [columns.values[2]]].sort end end |
#nm_table? ⇒ Boolean
True if table is a N:N link table. A N:N relation have at most one relation between two records. A N:N link table is also a M:M table
A table is a N:N link table if
o it has a primary key named 'id' as the first column
o has two reference fields using the link field naming convention
o has a unique index on the two reference fields
o and nothing else
38 39 40 41 42 43 44 45 |
# File 'lib/pg_graph/ext/meta.rb', line 38 def nm_table? @nm_table ||= @mm_table && begin expected_columns = referential_constraints.values.map(&:columns).flatten.sort unique_constraints.values.any? { |constraint| expected_columns == constraint.columns.sort } end end |
#path ⇒ Object
47 48 49 |
# File 'lib/pg_graph/ext/meta.rb', line 47 def path schema.name + "." + name end |