Can I reference an existing row in base table from inherited one?(postgres) - postgresql

I'm trying postgres, version 9.2
Can I reference an existing row in base table from inherited one?
Example:
i have a "person" table and a "student" table which inherits from person.
As i understand, if i insert new student, automatically new person is added.
But i need to insert first the person data, and then insert a reference to it and student related data.

As i understand, if i insert new student, automatically new person is added
No. Although you can select students by querying the person table, only the student table will be populated after an insert to the student table.
The relationship you want is foreign key not inheritance. Create the person id column in the student table and make it dependent on the person id on the person table.

Related

Master-detail custom database update sequence using entity framework

I have 3 tables with the relationship as: CompanyStatus and Employee is the child table of Company, CompanyStatus and Employee is not link to each other.
When perform insert data using entity framework, by looking at the profiler, i notice that the update sequence of entity framework as below
1. Company
2. Employee
3. CompanyStatus
The problem is: i have an insert trigger in Employee table which check the CompanyStatus of the related Company and base on that perform some database update action. So when the new row has been inserted into Employee table, the CompanyStatus is not exists yet. How can i change the update data sequence of entity framework and make it insert data with the custom sequence like:
1. Company
2. CompanyStatus
3. Employee.
Thanks in advance

eclipselink jpa joined table inheritance with mappedsuperclass

I have 2 tables. Employee and EmployeeDetails. Employee table has the basic details like Employee Id, Department and some audit fields like Created By, Created Timestamp. EmployeeDetails table has all the personal details about the employee and same audit fields (Created By, Created Timestamp) like Employee table. Now the audit fields and Version column are part of a MappedSuperclass ModelBaseFields.
I am using JOINED Inheritance in Employee which is my base class. It extends ModelBaseFields which is a MappedSuperclass. EmployeeDetails extends Employee.
Now the problem is, whenever I try to persist the data, Employee table INSERT query is formed properly however, EmployeeDetails INSERT query is missing audit fields (Created By, Created Timestamp) and version column.
I have tried using SINGLE TABLE inheritance with Secondary table. I am getting same issue in that scenario as well.
How do I add common columns in child table?

Entity Framework - How to Insert to table with foreign keys without retrieving foreign table rows first

I'm having a hard time finding the exact answer to this question, so my apologies if this is redundant.
So I have 3 tables defined such that:
Person :PersonId, FirstName, LastName
Company: CompanyId, CompanyName
Order: OrderId, PersonId, CompanyId
On the Order table, there is a foreign key defined on the PersonId and CompanyId columns, thus, my Order entity class generated by EF has a navigation properties of type Person (not PersonId) and Company.
So, to insert into the Order table, I first need to query the person and company tables to get the person and company entities. Then I can construct the Order object using the Person and Company entities and save it to the db.
In my scenario, I am being passed a PersonId and CompanyId.
In classic SQL I would just do INSERT INTO Order Set (CompanyId, PersonId) - 1 database call. But with EF, I have to do 3 db calls. This seems like overkill.
Is there any way around this?
PS - I'm using EF 6. I know I could generate an expression and make it single call..but that would still yield two subselects.
You can just include foreign key properties in addition to the navigation properties and then set them using the ids you have. If you do this will not have to go to the database to get related entities for just a sake of setting the relationship.

Setting the value of primary Key manually on Entity Model before insert

I have my Entity model where every entity has a primary key and they are marked with StoreGeneratedPattern = "Identity", the entity I am interested in is PEOPLE. It was created in oracle, and in another schema exists a "PEOLPE_HIST" table with it sequence. Before insert into PEOPLE I search on PEOPLE_HIST and if the person exists. I get the history information to including the ID (primary key) to insert into new PEOPLE table. I created a trigger for PEOPLE table to check if the ID is greater than or equal to 0, if it is, then I will not use the sequence and insert the Hist ID.
Is this possible?
thanks in advance

Need help with Entity Framework Relationshipships

I'm new to Entity Framework and just experimenting...
Consider 3 db tables where Person is a base table. I want the Employe table to enherit from Person, storing employee specific info. It seems that EF requires that PersonId also be the PK of the Employee table, so I made EmployeeID a unique index.
Next I added a table, Application, which stores one record for every software application that the Employee supports, creating a foreign key from Application.EmployeeId to Employee.EmployeeId.
However, EF doesn't seem to recognize relationships that involve unique indexes, but only Primary Keys.
What I can do is create a relationship from Application.PersonId to Person.PersonId, however, only Employees can be responsible for an Application, so it seems more natural to me to have Application as a "child" of the Employee table rather than the Person table.
Is this possible in EF?
You can build your relation between Employee (PersonId) and Application (EmployeeId). In such case the integrity should work as you expect because only PersonIds in Employee table will be only for existing employees. EF has currently no support for unique keys.