Multiple table foreign keys for one column.. Is it possible? - innodb

I have several entities, that can have multiple relationships.
For example, i have following entitites:
entity_type
tag
tag_assignment
news_post
account
In entity_type table are described all entities that i have in my project (e.g. news posts, blog posts, messages, accounts, everything)
Entity_type table has just id and name fields, name field describes model class name for usability
Tag entity has just id and name. It's standalone entity, that is mapped to other entities later with tag_assignment entity
Tag assignment entity has id, tag_id, entity_type_id and entity_id. Entity_type_id describes in which i can find entity, entity_id specifies entity in the table.
So i want to make following combined foreig key from one column to many tables:
tag_assignment.entity_type_id => entity_type (id)
tag_assignment.entity_id => news_post (id), account (id), etc
Is it possible to make this combined key? I mean if to make dependencies if i delete a row from entity_type table, everything will be dropped/updated in other tables, if i will delete account, only tag_assignments that have foreign key to account table will be deleted.

You should normalize your database by extracting a table called entity. The concept of this table is similar to an abstract class in OOP. The news_post, account and other entities you might have in your database should all reference the entity table. This way you can reference any entity that you have now or might have in the future with a common, specific location.
Also you might want to familiarize yourself with the EAV model. This might help you resolve similar design issues.

Related

Symfony2 - extending existing non-abstract entity?

Let's say I have a Setting entity with some fields like IntValue, dateValue, stringValue and some linked entities, like countries (ManyToMany to entity Country), languages (ManyToMany to Language) etc.
Settings are created by users and assigned to specific objects (not important here, but I wanted to clarify).
Now I suddenly need to have UserDefaultSetting, which will be the same, but with additional user field (ManyToOne to User entity).
I tried to extend existing Setting entity class with one more field added. The problem is, as I looked at the schema update SQL, it created new table for the new entity, but without all the tables needed to ORM connections (mostly ManyToMany). Just one table with "scalar" fields.
So previously I've had setting table with int_value, date_value etc. but also setting_country and setting_language tables, linking ManyToMany relations. After creating child entity, Doctrine created only user_default_setting table with int_value, date_value etc. and additionally user_id column, but I can't see any relation/link tables.
I know I should've been do it with abstract base entity class, but at the time I started, I didn't know that and now part of the project is on production (don't look at me like that, I blame the client) and I don't want to change that "base" class now. Can I inherit everything from non-abstract entity class in a way it will work?
UPDATE: everything explained. See Cerad's comment. Thanks!

Challenges with adding a 1-1 relational table (Users and UserSettings)

My issue is related to this question: Entity Framework One-To-One Mapping Issues
I have a Users table that already has a bunch of records.
Users (Id, UserName, Password, FullName, Gender)
I need to add a bunch of notification options for each user:
NotifyForNewComment
NotifyForNewPost
NotifyWhenFriendSignsUp
I may have to add more options later, but there will always be a 1-1 relationship, so my question is whether to store these in a separate table, say UserSettings, or just add them as columns to the Users table.
In the linked question (above), the advice was to create a new table and make the UserId in the UserSettings table as the primary key (because otherwise, Entity Framework doesn't like it). If that's what I have to do, then I have a few questions regarding it:
All my tables have an Id column. The UserSettings will not have an Id column then, since the UserId will be the primary key?
I'd have to turn on Identity Insert for the UserSettings table, so that when I insert a new User record, I can also insert a UserSettings record with the UserId?
Given that I already have a bunch of records in the Users table, what do I have to do if I'm going to introduce the new UserSettings table now which will have a 1-1 relationship with the Users table? Do I just run a script to add records for each user in the Users table with some default values? Or do I make it into a 0-1 relationship?
Since it's a 1-1 relationship, should I not worry about a new table, and instead just add them as columns to the existing Users table?
I think you are missing the point of a UserSettings table. It would have columns like:
UserSettingsId
UserId
Notification
It might also contain things like when the notification was created, whether it is currently enabled, and other information.
If you know exactly what the notifications are, and they are not going to change, then you might consider adding them as separate columns in the user table.
On the other hand, this is a natural 1-N relationship, and you should probably implement it as such.

How to remove the compound PK from a Symfony2 ManyToMany

I need to allow multiple Products to be present in a Cart. I do not want to increase a quantity column, I actually want the same Product entity in the Cart twice. I want to reuse the Product entities and not create a CartProduct intermediary too.
Cart ManyToMany Product
However, the table is created by doctrine:schema:update with a compound primary key of cart_id + product_id. This prevents me adding the same Product twice.
How do I solve this?
This is not the only use-case I have where I need a ManyToMany to support duplicate entries. Is this just not possible with Symfony2/Doctrine?
It's not so much a limitation of doctrine as it is of relational databases. Every row needs to have a unique primary key which, by default in Doctrine 2, would be product_id,cart_id.
The only way around it is to make yourself an explicit CartProduct entity and add at least one more column. Not that hard to do. Just establish OneToMany relations to it from Cart and Product.

LINQ to Entities, several one-to-one references to the same tables and naming

I've started porting a .NET SQL Server application to LINQ to Entities. I have (among others...) one table called Users, and one called Time. Time is reported on a specific user (UserId), but it is also recorded which user made the report (InsertedByUserId) and possibly who has updated the Time since insert (UpdatedByUserId). This gives me three references to the table Users.
When I generate a .EDMX from this I get three references to the table Users: User, User1 and User2. Without manual edit I have no way of knowing which one refers to the UserId, InsertedByUserId or UpdatedByUserId field.
How do others solve this? Maybe it's not necessary to register ALL references, and stick with InsertedByUserId and UpdatedByUserId as ints?
(The manual edit wouldn't be a problem if the database were never updated, but as we make changes to the database every now and then we occasionally have to regenerate the .EMDX, thus removing all manual changes.)
Thanks in advance!
Jos,
Generally when I make my foreign keys, I name them accordingly. From the Entity designer you can differentiate between the different Navigation Properties (ie User, User1, User2) by looking at the FK association (as long as you named your foreign keys distinctly). For Instance I have a ModifiedById and CreatedById field in each table. Both fields reference my SystemUser table, My foreign keys are named like this: FK_[TableName]_SystemUser_CreatedBy and FK_[TableName]_SystemUser_ModifiedBy.
You should notice that in the Navigation properties you can see the Foreign key. You can also modify the name of the Navigation Property (which is in the Conceptual Side "CSDL portion" of the EDMX), and this change will stay when you update your EDMX from the database.

asp.net MVC fetching data with table relations

I have created my Database which has:
Artist, tracks and TracksPerArtists
I can do sql queries through the entity model. For example when I make:
database.TRACK.ToList()
I get the list of track to be shown on index view for example. But my foreign keys come empty. While there is an artist and a track, and the correct row for ArtistsPerTrack, this item in my track.ToList() collection is empty.
Is there a different way to fetch those data?
I came from cakePHP framework in which you can define the Model.recursive property to declare the depth of the relations you want to fetch.
Is there anything similar here?
There is something similar.
If "database" is DataContext and Artist and Tracks have many to many relationship in database, you can fetch related entities using Include clause:
database.TRACK.Include("Artists").ToList()
where "Artists" is the name of property on Track entity.

Resources