how to make multi_friends relationship - sql

I have database contains those tables:
user, post, like and comment.
I was able to make diagram to show relationship between them.
one user has many posts, many likes and many comments.
one post or comment has many likes.
post can have comment also.
see diagram :
my question is how can i make friendship for user, (user has many friends)
please give me a diagram or strategy to do it, thanks.

Table
FRIENDS
---
SOURCE_USER_ID FK to USER
DEST_USER_ID FK to USER

Related

How can I fix the rendundancy in my ER-Diagram?

I've a problem with my ER-Diagram, I hope somebody can help. The scenario: users can create contacts. The user can also add notes to the contact. The note consists of one text field. The contacts can be connected to the notes, that means; one contact can be linked to several notes and one note can be linked to several contacts. Now I have an entity "User" and an entity "Contact", it's a many to many relationship. But I'm stucked at one point: I'm not sure if "Note" also have to be an entity, because I've to show the many to many relationship between the notes and contacts, but I've also to show the relation between the user and the note. This would lead to a circle which is redundant. Any help would be appreciated thanks!
You definitely need to show note as an entity. Now, since you already defined a relationship between contact and note there is no need to show another relationship between person and note. You can get note for a user with user and contact combination.

Database Schema for Individual and Group accounts(Like Facebook Users and Pages)

I'm trying to create a schema, which is very similar to part of what Facebook does,so i will explain with that as an example as it would be easier.
Just like Facebook, we have Users and Facebook Pages. Both can publish posts, follow each other and the users have a feed page where they can see the posts of the entities they have followed(User or Pages)
The page is managed by group of accounts(page moderators),the posts they make will appear as posts from the page.
How do i go about this?
What i thought of
Create a user table and Page Table. User table have a column called group_id which will map to the Page Table id for the moderator accounts, since both are kind of similar this makes sense.
Making separate User and Moderator Tables
However with the above approach i probably would have make two separate tables for posts as author_id will be different, users will have user_id and pages have page_id.
Also thinking of doing something like this for the Follow table
follower-user_followed-page_followed
1-2-NULL
1-NULL-5
I know this is not the kind of ideal solution and will be problematic to retrieve the user_id and page_id from the Follow table, do a query on posts from user post table and then again on brand post table, merge them all and show it on the feed,but i'm unable to wrap my head around this any other way.Searched for Facebook schema, but was unable to find with Pages in it. Please any pointers would be helpful.
You should use different tables for different entities.
Users
Groups
Moderators (contains id of corresponding users and moderated by them groups id)
Followers (contains id of users and followed by them groups)
Tables 3 and 4 provides such called "many-to-many" relationship (multiple users can follow multiple groups), as databases support only one-to-many or one-to-one relationship from the box

Activity streams / feeds / news in social network database schema

I have a goal to implement database schema for simple \ typical social network.
I have read many threads \ answers but have couple open questions.
So we have User table (userId, name and etc). We can make some Actions (reply, like, follow and etc). I want to implement some log for all activities and do it as PULL-MODEL. So we write entry in Activity table for any action. Schema for this table is (id, ownerId, actionType, targetId, time) where ownerId is User's id, who made action. actionType is reply, follow or other action. targetId is id of user or post and depends on actionType. When User get his activities we just do query by friends ids. So it is clear for me. My questions are:
1) In case if I follow User and unfollow him, what I should do? Should I make two entries in Activity table or I should remove the first followAction entry? What is the best practice?
2) It is clear foe me do query by friend ids so I get all activities of my friends. But in case any not my friend liked my photo and I must get event that "Some not my friends liked my photo". So, what are good solutions there for this case. May be I must to change my current schema?
Releated questions :
How to implement the activity stream in a social network
Database Design - "Push" Model, or Fan-out-on-write
What's the best manner of implementing a social activity stream?
Thanks you all for good answers.
First, it may be better to split each kind of action into its own table, rather than having all actions in one table, distinguished by types. This makes your metadata about each action more flexible; as you say, the target ID depends on the action; without splitting them out into other tables, it's harder to write constraints on what the data should be.
Second - on your question #1, I think you're confusing a log of user actions with user status. You may need both; you might want two separate data structures. For example, if a user follows and then unfollows, the status is that they aren't following, but the log of actions is that they followed, then unfollowed. So I think you should be careful to have a separate data structure that captures current status of certain relationships, apart from actions. Then the problem becomes simpler, you log all actions as they happen, and update status accordingly.
For question #2, the photo should be its own data object, with "likes" split out into a different table; users like posts. Then of all of the users who like a post, they can easily be grouped into two categories; friends (those who have a friend relationship to the poster) and non-friends.

Storing Blog Comments/Upvotes - Tracking Users?

I am working on a blog-type website in ASP .net MVC3. I am trying to figure out how I will deal with post upvotes/downvotes(I will have to know what users have already voted where to prevent spam voting). Comments on a blog post is another issue.
My thoughts so far(I am sure they are pretty far off the mark):
Votes:
Store a list of UserIDs in a voted field of my Blog table.
For each user in my Users table, store a list of all PostIDs they have voted on.
Comments:
Make a separate Comments table and in that table have a field referencing the parent blog post.
Store a list of CommentIDs in a Comment field in my Blogs table.
I know there are several other ways to go about this but I am trying to set this up so that I won't have to rewrite the whole thing should I get an influx of users.
You might wanna consider creating a Votes table like
User|Post|Type?
john|43 |Up
mary|43 |Down
making User + Post a composite primary key, and thus indexing by both... Then you can easily check if a user has already voted for a post or not... You can also create additional indexes by user or post if needed...
I'd also be a good idea then to have the "Current Ups and Current Downs" in the blogs table, so you don't have to count them each time...

Rails 3 - Many to Many - How would you do?

I have found lots of answers on StackOverflow but i'm kinda stuck on this one
I'll try first to describe with words what I have to do:
I have multiple applications, each application can have one or multiple profiles (one to many).
I also have users, who have access to each applications through the different profiles. Each profile can have multiple users (many to many).
up to here no problem, i can get all profiles a user has been granted.
However, the difficulty here is that for each profile coming from an application, the user has a username, specific to each applications. When i see the details of a user, i want to see a list of all the profiles he's in together with the username he has been assigned for each application...
I'm sure there an easy way to do this with rails, as usual, but i can't seem to find it. How would you do this ?
So to make sure I've got this: an Application can have many Profiles, and Users can have many Profiles. So this isn't a simple many-to-many relationship between Application and User because the Profile is a first-class object.
Rails handles simple many-to-many relationships with the has_and_belongs_to_many (HABTM) association, declared on the models on both ends. What's in the middle is unimportant and merely serves to join (relate) the two models.
Your case is more fun. Your many-to-many is described in Rails as "has_many :through", and I think your case is a very good example of such a case. In this case, Application and User each have many of the other through the Profile model. Profile isn't there just to link the two, it holds username, and probably many other details of the User's relationship with his/her Applications.
Start with this excellent guide which should show you how (and why) to choose has_many :through and how to get it all modeled and set up. This is (as you suggest) one of the absolutely brilliant capabilities of Rails.
I hope this is helpful.