Postgraphile: multiple foreignkeys on same table with smart tags and postgraphile.tags.json5 - postgraphile

NodeJS Project with apollo/postgraphile
I would like to add multiple virtual foreignkey on multiple tables with postgraphile Smart Tags using postgraphile.tags.json5
If i do this in postgraphile.tags.json5
tags: {
foreignKey: "(col1_id) REFERENCES table1 (id)",
foreignKey: "(col2_id) REFERENCES table2 (id)"
}
Then only latest foreignKey tag will work.
How can i manage to add multiple foreignKey tag referencings columns from differents table ?

Related

One-to-many relationship without actual foreign key

I want to make a one-to-many relationship like in the tutorial.
~~~
One To Many
An example of a one-to-many relation is a blog post that "has many" comments. We can model this relation like so:
class Post extends Eloquent {
public function comments()
{
return $this->hasMany('Comment');
}
}
~~~
So i want to make a Model Post (connected to table "posts") and a Model Comment (connected to table 'comments'). I am creating the tables in phpMyAdmin and not with migrations (because i have no SSH support on the online server). The comments table has a column 'posts_id'.
Can i use ...
$comments = Post::find(1)->comments;
..without defining a foreign key relationship between the two tables in phpmyadmin?
And if the answer is YES.
Should i make a column name "post_id" or something like this in my 'comments' table or something for this to work? Just like you would do with a normal foreign key?
You don't have to explicitly declare a foreign key in the MySQL side but you have at least to create a post_id column that will be used by Laravel as a foreign key.
Of course, you can name this column as you want and specify it in the declaration of the relation :
class Post extends Eloquent {
public function comments()
{
return $this->hasMany('Comment', 'post_primary_key');
}
}
You can also declare this column as a foreign key in PHPMyAdmin to improve robustness of your database but that's not Laravel business.

Inserting using id in many-to-many relationship in SQLAlchemy

Suppose I have an Article model and a Tag model in a many-to-many relationship using an association table. The tag has attributes id (primary key) and tag_name. Articles have many tags.
I want to create a new Article with tags, and I have the ids of existing tags to be added.
I have something like this:
self.tags = [ Tag.query.filter_by(id=tag_id).first() for tag_id in tag_ids ]
(I am using Flask-SQLAlchemy.)
How do I do this without unnecessarily querying for the Tag objects?
I want to in effect just insert the tag_id, article_id association in the association table; can I do this without using a raw statement?
If you have the association table mapped directly, like TagAssociation, then you can just create TagAssociation(article_id=x, tag_id=y). If OTOH the table is only present in the secondary argument of relationship(), the ORM doesn't have a direct system, you'd need to run an insert(), though you'd use Session.execute(), that is, Session.execute(assoc_table.insert(), params={"article_id": x, "tag_id": y}).

How to set up intermediary table in MySQL for ManyToMany relations in squeryl for Play 2 framework?

I'm still learning all the ins and outs of web development, scala, play 2.0, and squeryl and I'm trying to set up a ManyToMany relation between two of my tables.
I've looked over the information found here but I'm having trouble with the intermediary table. I've looked all over and I can't find a good example of how it should be structured.
I'm using MySQL for my database and I've tried using foreign keys and primary keys in the intermediary table, but neither have worked, or maybe I'm just doing it wrong. So, could someone give me a clear example of how the intermediate table should look?
For a little more information, the basic structure of the two tables I want to relate are as follows.
tableOne (
name varchar(255)
);
tableTwo (
name varchar(255),
idCode varchar(255)
);
They will be related by the name in tableOne and the idCode in tableTwo which is just an abbreviated form of tableTwo's name column.
So using MySQL, squeryl, and the format shown in the link, can anyone help me get this going?
To relate the two tables, you will need to establish a ManyToMany relationship in your schema. Assuming you have defined your tables with the names tableOne and tableTwo in your schema, something like this probably what you want:
First create a class that joins the two tables:
class TableOneToTwo(
name:String = "",
idCode:String = "") extends KeyedEntity[CompositeKey2[String, String]] {
def id = compositeKey(name, idCode)
}
Then map the relation in your Schema
val tableOneToTwo = manyToManyRelation(tableOne, tableTwo).via[TableOneToTwo]((t1, t2, jt) => (t1.name === jt.name, t2.idCode === jt.idCode))
Then you would just need to create the corresponding table in your DB - which should have two fields - name, id_code (named according to your naming convention).
If you need a reference, this should point you in the right direction: http://squeryl.org/relations.html

Indexes on many-to-many table

current setup is:
objects (notes, reminders, files) - each in separate table
entities (clients, projects) - each in separate table
object can belong to many entities, entities can have many objects
associations table looks like this:
object_type_id, object_id, entity_type_id, entity_id
How would you handle indexes on associations table? Any comments about the setup?
I'm not that strong on databases in general. But i usually allways index any field that is a id reference to another table.
So i'd probably index all the fields in your associations table, since they all refer to data in other tables (or so i assume).
You should probably also add a Primary KEY id to the associations table, so when you wan't to delete an association you can do it via a primary key reference.
With mysql, if you've defined the foreign keys as actual RI foreign keys using the references keyword, you get an index automatically defined on the table. And primary keys also get an index, so you shouldn't have to define any indexes manually.

Possible to Fix a Foreign Key in Yii without having set it up in the Database?

I'm using phpMyAdmin for my database GUI and it's connecting to Yii Framework on my website.
I wish for my products table for instance, to have a foreign key department_id which is a reference to the id field of my departments table. Unfortunately, I don't currently have the facility to set the foreign key up properly in phpMyAdmin, so department_id is just an indexed field. I cannot change the configuration of phpMyAdmin at the moment so it's stuck like it is without a foreign key and relationship facility.
Is there a way to modify the Models of these tables in Yii so they link? I know there is a relations function inside the Model Class file that holds this information:
return array('department_id' => array(self::BELONGS_TO, 'Departments', 'id'),
Could I not just add something similar to the above? Is there more legwork? Is it now fixed (as in static, not corrected) because of phpMyAdmin?
Cheers
If I'm not mistaken, you don't need to have mySql enforcing foreign key relationships for them to still work in Yii. Setting up FK constraints in mySql ensures proper database integrity, but I don't think Yii actually uses that at runtime.
When initially running yiic (of Gii) to build the project I think it looks at the DB to build the right relations in the Model, but it doesn't use them after that.
Yii then uses this knowledge (from yiic) of the table relationships to make your life easier by providing shortcut methods to access relational data, and to ensure you don't violate mySql constraints and get ugly SQL errors, etc. But you can still use Yii relation logic without the underlying SQL constraints. The only problem will be that if Yii messes up and assigns a non-existing FK or something, your database will not catch this error (your data integrity will be more error prone).
To link your products to departments, just make sure you have a department_id field in the Product (which it sounds like you do). Then add a relation rule like so to Product:
'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
And in your Department model:
'products' => array(self::HAS_MANY, 'Product', 'department_id'),
Now you should be able to use the relation like normal:
$myProductModel->department; // returns the model of the Department referenced
$myDepartmentModel->products; // returns the models of all Products in the department
Good luck, and let me know if I am way off base and it doesn't work for you!