Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table relationships question #3

Open
samlevin opened this issue Apr 26, 2015 · 3 comments
Open

Table relationships question #3

samlevin opened this issue Apr 26, 2015 · 3 comments
Labels

Comments

@samlevin
Copy link

How can I set 'foreign key' constraints in MySQL using JSONModel/SQLAbstract stack? These are important for my schema. Can I do it?

@laurentszyster
Copy link
Contributor

There is nothing (yet) in JSONModel nor SQLAbstract to set foreign key constraints.

To add support for foreign keys would imply two modifications:

a) Add a constraints argument to SQLAbstract->createTableStatement
b) Add a foreign property to JSONModel and support for a foreign option in its constructor.

Instead of:

SQLAbstract->createTableStatement ($name, $columns, $primary)

We would have:

SQLAbstract->createTableStatement ($name, $columns, $constraints)

Where $constraints is a simple list of SQL constraints.

The definition of PRIMARY KEY constraint is moved outside of SQLAbstract.

In JSONModel.

Were a foreign option is added, as a mapping of column name(s) to the foreign JSONModel(s):

function tagsTable (SQLAbstract $sql) {
    return new JSONModel($sql, array(
        'name' => 'tags',
        'primary' => array(
            'tag_task', 'tag_label'
        ),
        'foreign' => array(
            'tag_task' => taskTable($sql)
        ),
        'types' => array(
            'tag_task' => 'intval'
        )
    ));
}

Besides generating FOREIGN KEY constraints, this foreign option can also be used to query related models (without having to specify the relation outside of the models definitions).

@laurentszyster
Copy link
Contributor

see: unframed/SQLAbstract.php#4

@laurentszyster
Copy link
Contributor

Having a foreign option (that defines foreign key constraints) also enables models to relate selections automatically.

With the above example, for instance to relate tasks to tags selected by safe options:

$tagsTable->relate($options);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants