I have two preexisting tables that were created by a CMS. I've mapped them in Symfony2 and I am trying to establish a relationship between two of the tables but I am now noticing that the CMS created these tables without a foreign key. This table table_uploads has a column that every other table is related to called column_table_name and column_record_num.
So assuming we have three tables... table_students, table_uploads, and table_teachers, the table table_uploads will have a value of either students or teachers in column_table_name to show its relation to the other two tables as well as a number in column_record_num that corresponds to the other table's primary key. Neither of the three tables actually have a defined foreign key however.
In Symfony, I'd like to call a table
$students = $this->getDoctrine()->getRepository('SchoolBundle:Students')->findAll();
and then load the related records from another table in such a way like (I know select_related doesn't actually exist, but this is essentially the functionality I'm looking to create)
$students->select_related()
Is this at all possible without the existence of a foreign key? The only way I can think of doing it is grabbing the column values from $students and running another query to other table with these values and merging the two results.
You could create a custom repository method that would join unrelated tables. DQL join between unrelated entities?
Related
I have multiple tables in a Laravel app with 1-to-1 relationship such as users , users_settings , user_financial
And some 1-to-many relationships such as users_histories
My questions are:
1. Should I always include incremental id at the first?
for example is the id necessary in the Table #2 below?
Table 1:
id (primary,increments) , name, email, password
Table 2:
id (primary,increments), user_id, something_extra
^ why does every guide include this? // e.g. https://appdividend.com/2017/10/12/laravel-one-to-one-eloquent-relationships/
Can't I just use user_id as primary key and skip the incremental key? because I want to auto insert it on table 2 as soon as data is inserted in table 1.
2. How should I name 1-to-1 and 1-to-many tables in Laravel? `
I searched but didn't find any naming convention for different type of relationships...
Currently I do:
users table with primary key id is the base.
1-to-1: users_settings with foreign key user_id
1-to-many: users_histories foreign_key user_id
many-to-many: users_groups foreign_key user_id
should the first two tables be named settings/setting , histories/history instead? sorry I'm a little confused here.
I actually asked a similar question around 2 days ago. Its up to you but I'd say yes. In my case if I don't auto_increment all my ids in the related tables, data won't be associated with the correct user. However, there is an argument for saying auto_increment columns should not be used in this case, but they are useful for other things. According to some, the relationships might not be as meaningful so it'd be up to you and down to the specifics of you data tables for how meaningful the relationship will be. Regardless, you should research more into the advantages of auto_incrementing all your ids in related tables, as well as possible disadvantages before deciding what you want to do. Either way is fine, but they offer different advantages and disadvantages- which you'll need to compare and what works best for your specific case.
This is a well debated topic about the primary key. IMHO, No, you shouldn't. Every column in database should have a purpose. Following this, for your example, I agree that the auto_increment id is redundant and this is simply because it doesn't have a purpose. The second table is still uniquely describing the user so that the primary key should be the user_id.
Beside the above, there is another principle for me to decide whether I need the auto_increment id: whether I can see a table as an entity. For example, user is clearly an entity, but a relationship is not (in most cases), i.e., composite key can serves the purpose. But when an relationship table is extended to have more attributes and it starts to make sense for it to have an auto_increment id.
I don't have much experience on Laravel, but the naming for a database table should not be dictated by a framework. Comparing history and user_history, what a new DBA or developer expect from the two names without looking its data? user_history describes the table more precisely
I have a database that needs migrating into a completely different structure (we're going from a custom CMS to a slightly modified Wordpress DB).
I can pull the individual tables and map them onto the new structure easily enough, but I'm struggling to find a method to preserve the relationships between tables.
For example in the old db I have a customer and basket table joined 1 to many from the customer table. (IE one customer - many baskets). Mapping these to the new db is simple (user, usermeta and a custom table). However the keys in the new db are completely different to the keys in the old db, so I cannot use them to relate the new records together.
How can I re-establish this relationship? (even broad terms will be helpful)
The only thing that occurs to me is keep your old keys in "temporary" columns.
Create the customer table first, with new id column, keeping the old id column.
Now create the basket table (without
the foreign key constraint).
Run an update query, to update the new basket
key column with the new customer id, joining on the OLD keys (sorry, don't know MySQL update syntax well enough to give an example).
Recreate the foreign key constraint on the new columns
Drop the "old" columns
Hope that helps.
Suppose you have a table A related to another table B by a one-to-many (hasMany) relation. Table B can have other hasMany relations and so on.
For instance, I am trying to do it in cakePhp, but I think it is a general SQL problem.
Suppose that in every table there is a column, user_id.
There is a way to update all the columns user_id in all related table with one SQL construct? Or rather, which is the fastest way to do it? Now, I could not find a solution but doing it by iteration, and, obviously this is quite time-consuming and server-overloading.
Cake has its conventions for automating some functionality interaction between it's models and the tables they reference. Foreign keys should be called people_id, if they reference a table called people. How do I handle a case where a row holds two people, and needs foreign keys for both people? It's obvious that I can't duplicate the column names, having two people_id columns.
Consider a competition where you have two competitors. Each competitor is represented by a row in the competitor's table. The competitions table needs to refer to both of those competitors. How can I do this wouldn't breaking the automatic functionality that Cake offers?
I found a link that describes how to do this perfectly.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm
Lately I have been doing some work with MySQL and have come across the need to store table names as a field in one of my tables (which is not ideal). Instead I would like to reference a list of tables that currently exist in the database as a foreign key. I have found that the information_schema database stores a list of these tables however I cannot reference them as a foreign key as they are Memory tables. Is there another way I can reference the list of tables in the database as a foreign key.
For those interested, the reason I need to store the table names is because I am developing a basic PHP framework that exposes each table in the database as a class.
How dynamic is the list of tables in your database? One option is to create your own table and (periodically) fill it with the table names from information_schema.