How to map relationship between these entities -Coredata iOS - ios

I have two entities Account and Transaction. Following conditions are applicable for the db.
Each Account has different transactions
allTransactions has to many relationship with Transaction table
Each transaction related to one account (Debited from Account/ Credited To Account), accounts has to one relationship with Account Table.
Amount can be transferred between the accounts. (Transaction)
Problem: How can i relate the transaction table with Account Table for the above (point 3) condition
My Coredata Structure

If you are transferring an amount from one account to another account, you need references to two accounts.
Transaction
- fromAccount <<--> Account
- toAccount <<--> Account
On the Account side you will need two inverse relationships.
Account
- outgoing <--->> Transaction
- incoming <--->> Transaction
So you do not need the property accounts. (It is also confusing to name a to-one relationship in the plural.) Similarly, you will not need allTransactions.

Related

User and Customer in ER diagram

I'm having a scenario to create an ER diagram.
Scenario
There are Several Regions. One region can have several Business Chains. Each Business chain have one region to cover. One Business chain have several outlets. Customers can use this system to connect to any Business chain. Employees of a Business chain can be assigned to any outlet by the admin of the particular Business chain................
My Question is how am I suppose to handle user details and login details in the above ER diagram (or in the application)
Should I use two separate entities as "Customer" and "Employee"???
Should I use one entity as "User"? If so how to handle the above case of handling emplyees' roster
I suggest you combine customers, employees and users into persons, and use subtyping for each of the roles in which a person may occur:
I left out any indication of overlapping/disjoint subsets, you can fill them in based on your requirements. Implementing disjoint subsets would require adding some additional type indicators and check constraints to the tables below.
Physically, the diagram above would translate into a set of tables like:
person (person_id PK, first_name, last_name, ...)
user (person_id PK/FK, username, password_hash, ...)
customer (person_id PK/FK, credit_limit, ...)
employee (person_id PK/FK, salary, ...)
This allows any person with a user record to log in, and you can easily find out whether they're customers, employees (or both) by joining with those tables. You can create customer or employee-specific relationships easily, e.g.
outlet_employees (outled_id PK/FK, employee_person_id PK/FK)
where employee_person_id has an FK constraint referencing person_id in the employee table. You can also make user-specific relationships, or general person relationships, as your requirements dictate

Relationship query: A company has many employees

Lets say a relationship is described as:
A company has many employees
A company has many departments
A department has many employees
So, something like this;
Company -<< Departments >>- Employees
If department table has a basic structure of:
// Pseduocode
company_id // Foreign key
department_id // Primary key
[employees] // Array or collection of employees
If we go back to this phrase;
A Company has many employees
Does this mean that the employee table needs or requires a reference for the company_id too?
So, Employee would be:
employee_id
company_id // I'm not sure if this is requried or not
department_id
I'm intending to hopefully abstract this data into a contracts table in case of employees are freelancers/contracters, etc or have multiple employees.
But for now..
My question is:
Does my employee table require a reference to the company table, or is the company reference implied via the department table?
Many thanks
Question is: do you need a straight connection between Company and Employee? If you do, add it, if not then yes, the connection is implied through Department.
EDIT:
Technically, your Department table does not need a list of Employees. Each row of Employee table has a reference ID to Department, that's enough.
Check this out for more information.
The relationship is implied through the department, so you don't conceptually need it. Adding it, would be an example of denormalisation, and would allow inconsistencies to crop up. For example, you might have company_1 with department_1 and company_2. Now employee_1 might be linked to department_1 but company_2, because of some flaw/bug in the application code. There's no way to express this constraint as a SQL schema, so you'd have to go with more complicated stuff such as triggers, or application code checks etc.
However, sometimes you only need info about companies and employees, but not about departments. If it's really performance critical, doing the extra join with departments in order to find the company for an employee or the employees for a company might not cut it, so you'll just have to live with the denormalisation.
You probably do not need to link Company with Employee. The relation between Departement and Company already do the job. You may need it only if there are existing particular cases requiring this relationship.

ios Core Data Design Issue

I am trying to create a Core Data model for my application. I have having issues in defining the attributes and relationships. Here's the scenario
I have a Contract Management Application. So the basic entity is Contract. It has a Tenant but can have multiple Landlords. Tenant and Landlords can either be a Person or a Company type.
I have defined the entities as shown in the table below.
I only created Landlord and Tenant to reference them easily from Contract. I feel that Contract should have a To-Many relationship with a name of landlords as a Contract can have multiple landlords. The problem I have is that each Landlord could either be a Person or a Company. Same goes with the tenant, though it would always be one for a Contract.
Here's the updated Snapshot of Landlord
& Tenant Entities
Can you please help me define what relations should I create from Contract to Landlord && Tenant ( along with reverse relations if any ) ?
Here's the ER Diagram that I could think of my head ! ;)
Can you please help me define what relations should I create from Contract to Landlord && Tenant ( along with reverse relations if any ) with each of them being either a Company or a Person ?
Thanks
I've set up a few data structures similar to yours.
What works for me in this situation is to set up an Entity that defines the roles that each person plays in a contract and another Entity that defines type of role.
So in your case I'd think about using this structure...
Delete the Entities Landlord and Tenant;
Add a new Entity called ContractRole with Relationships contract, company, person and contractRoleType.
Add a new Entity called ContractRoleType with Attribute type and Relationship contractRole;
to the existing Entities Contract, Company and Person, add a new Relationship contractRole;
the Entity ContractRoleType will have at least two types "Landlord" and "Tenant".
So for each Contract you will enter data for at least two ContractRoles, one with ContractRoleType "Landlord" and one with ContractRoleType "Tenant".
I find this type of data structure very flexible.

Core Data Programming Guide: intermediate join entity example, MUTUAL or NOT?

In the example of using "intermediate join entity"
To find out who one person’s friends are, you have to aggregate all the friend destinations of the friends relationship, for example:
NSSet *personsFriends = [aPerson valueForKeyPath:#"friends.friend"];
Is above line of code getting a given person's MUTUAL friends? or just ONE-WAY friends, which means only getting "peoples who are treated by this given person as his friends"?
I am not certain, because "To find out who one person’s friends are" sounds like ONE-WAY friendship (that could be why there is a strange relationship befriendedBy represents those who count the source as their friend. FriendInfo represents information about one friendship, “in one direction.” .)
This is a really confusing example. There are two possibilities.
In most cases being "friends" is a mutual thing. In this case you would have a self-referencing many-to-many relationship of a Person, perhaps called friends. The relationship would be mutual.
You seem to be implying that it is possible to add another person as a friend even if that person is does not reciprocate. In order to lift the confusion give this many-to-many relationship another distinct name, e.g. contacts. This would be the Persons that have been added unilaterally. In Core Data, all relationships are best modelled as mutual, so you can use another relationship potentialFriends that is the inverse relationship of contacts. Maybe there are better names, but you get the idea.
The intermediate join entity is only necessary if you want to store additional attribute with a particular relationship, e.g. the date a contact request was made. In this case, you would have the join entity e.g. friendLink, which would have a to-one relationship to two distinct Persons. You can model the state of the link (unilateral or mutual) in this entity.

Modelling Company and Person Customers in Rails

My Rails application uses STI where I have different types of companies and persons. For example I have suppliers, manufacturers and customers as types of Company. I have also employees, contacts and customers as types of People.
Now I want to refer to a Customer which can either be a Company Customer or a Person Customer. Which method can I use/should I use to combine these two different entities into one? So I can refer to a Customer, from an Order?
You could either use:
Order
belongs_to :company
belongs_to :person
end
And have two foreign keys - and then add some validations to make sure one of them is filled in, and then maybe add a 'customer' method which returns either the related company or person, depending which is used.
OR, create a separate Customer model (and table), which has those two same two foreign keys, and then Order can simply belong_to :customer. This second method may be preferably if you want to store additional data at the customer level (such as credit limit, or billing details), and may be cleaner long-term.
Alternative, you could reconsider your business logic, and insist that all orders belongs to a Person, even if that person is an employee of a Company and is purchasing on behalf of the company.
F

Resources