Can ActiveRecord HasMany/Belongs To use a non-PrimaryKey Column for the relation? - castle-activerecord

I'd like to have a parent-child hierarchy AND I'd like to have the concept of a "series-id" on the parent.
So the parent (if you updated it) would expire the old unique key.. keep the series id and insert a second row.
I'd like the children to "hang" off that series id rather than the unique id.
Do you know if it's possible for BelongsTo to link into a non primary key (yet unique via GUIDs) column?
Parent:
UniqueId (guid), SeriesId (guid), Name, Description
Child:
UniqueId (guid), ParentId (always connects ot UniqueId-Above), Name, Description.
I know that foreign key relations really should have a "key" on one side of the relation however, by my GUIDs they are key-like.. but not really keys.
Its not a big deal but if I "expire" the parent by updating it's metadata i'd like to avoid having to "copy" all the children into the new copy of the parent.
Am I trying to do something that can't happen?

There's an option for belongs_to that lets you specify the relationship:
:foreign_key
Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" will use a foreign key of "favorite_person_id".

Related

Have I done this database normalization question right?

I'm learning database normalization, got a question but don't know if I've done this correctly or not.
Here is my solution:
According to my knowledge your solution is not good
My Solution
Simply I change your data like this
Now I can see this relation is in 0th normal form
So remove name column and created new relation like below
Now customer relation is in
1st normal form because no multi-valued
2nd normal form because no partially dependencies
3rd normal form because contains only columns that are
non-transitively dependent on the primary key
Shopper relation is like this, but it is in 0th normal form yet
Now I remove purchased_item column from shopper relation and create new relation as below
Now Purchased relation is in
1st normal form because no multi-valued
2nd normal form because no partially dependencies
3rd normal form because contains only columns that are
non-transitively dependent
So now I update shopper relation like below
both ID and Item_ID are primary keys (Composite primary key)
Now customer, purchased and shopper relations are in 3rd normal form because no multi-valued attributes, all are fully dependent on primary key, no transitive attributes.
Final relations:
Customer (ID, Name)
Purchased_items (Item_ID, Item_Name)
Shopper (Customer_ID, Item_ID)
My bad, I forgot to specifically mention that in normalization we can't add new attribute or drop attributes. So in here I simply change your data relation into a more understandable relation only. Thank to #Mike Sherrill 'Cat Recall for pointing this issue.

laravel select specific columns but with relationships

I'm currently optimizing my application and one thing is selecting only the things from database which are really needed.
For example when I'm retrieving a User object I normally do it like this:
$user = User::where($attribute, '=', $value)->first();
This code will give me back a full user object with all the columns. My users table is pretty big, so I thought it is a good idea to only select the columns which I need like this:
$user = User::where($attribute, '=', $value)->first($columns);
This is also working as expected. But my problem is, as soon as I specify columns, the relationships of the $user variable are not working anymore, because it is no longer an object.
My question: Is it possible to select only a few columns but still be able to use relations like
$user->parents
Thanks!
Try This
$user = User::where($attribute, '=', $value)->select($columns)->first();
Just add all foreign keys for the relationships you want to get. For example, if you want to use parent() relationship, just add the key to the $columns:
array_push($columns, 'parent_id');
After that, you'll be able to use the parent() relationship.
Yes it is possible to load relationship with selected columns but for that you have to select foreign keys of that relationship table.
So here you also have to select the column of user table which is foreign key in parents table and also mention relationship using with(),
For example if you have user_id column in parents table as foreign key then you have to select id column of user while you are mentioning columns array in first() method as below:
$user = User::with('parents')->where($attribute, $value)->first(['id','email','image']); // also select 'id' for foreign key to load relationship
After using above code you can get relationship of parents as: $user->parents, and don't forget to put relationship in your User Model.
If you have foreign key on user table as parent_id which belongs to parents table then you have to select that parent_id column as below code:
$user = User::with('parents')->where($attribute, $value)->first(['parent_id','email','image']);

Database table design - parent child record spin off from main table?

I have a table "person", implementing a field as 'SupervisorID'.
PersonID
PersionName
DOB
Address
SupervisorID
If SupervisorID field is empty or NULL means he/she is supervisor or no supervisor at all.
I am thinking of to spin off as separate table, eg. table 'supervisor' to indicate this parent-child relationship.
Table 'supervisor':
SupervisorID
PersonID
For those have no supervisor will not appear in this table.
As stated, you are trying to create a many-to-many relationship (that is: employee can have many/one/zero supervisors).
For simple one-to-many relationship the additional table is usually not needed, if it only contains (SupervisorID, PersonID). It becomes useful when a relationship has its own attributes. For example (SupervisorID, PersonID, DateStarted) would be useful to neatly store the additional attribute: date when employee-supervisor relation started.

Cakephp, HABTM association

Cake newb here.
I have two tables. Users and Events. An user can subscribe to Multiple events.
What is the best way to implement this?
Do I have to create another table and link them or is there any other better approach.
If I do create a new table, how do i link them in cake model?
As said by jQuery.PHP.Magento.com you should use HABTM relationship but the name of the third table should be events_users because the table names should be in alphabetical order.
From the doc:
Table names are in alphabetical order by convention. It is possible to
define a custom table name in association definition.
You should use HABTM relationship.
Reason
See users will subscribe to Multiple events and
One event have multiple users subscribed for.
So this is two way relationship. Therefore you need following tables
users : To store user's data,
events : To store user's data,
events_users : To store Which user joined Which event and Vice versa(Events with n number of users)
So users_events will have 2 fields user_id , event_id , both are foreign keys and here you dont need primary key in HABTM relationship.

Inversed FK with NOT NULL required

I am trying to model a database where I have 2 inverted foreign keys which both tables fields are required NOT NULL. How can I model that?
Here a small sample:
I have two tables called the first is a generic called "SystemObjects" and the second is called "Users". Each "SystemObject" is created by a "User" object and so require a 'CreatedBy' foreign key which cannot be null. In the same time, all users have also generic associated SystemObject entry and the table "Users" contains more info about those objects with specific type "User".
In OOP analogy, "User" inherits from "SystemObject" and has supplementary members. and so I need to keep track of that connection.
A summary to my problem:
"A" has a foreign key field to "B"
"B" has a foreign key field to "A"
Both are required (NOT NULL)
How to escape this trap? Any alternative Model that would solve my problem and allow me to have this vis-verca tracking?
Thank you!

Resources