Implement relation conditionally at entity level in typeorm - postgresql

I have a case where I need to implement the softDelete feature of TypeORM. Somewhere in my entity(let's call it Lead), I have a column that maps to another entity(let's call it Customer) with OneToOne relation.
............
#OneToOne(type => Customer, {})
#JoinColumn()
customer: Customer;
..........
The problem here is since soft remove doesn't remove the record completely from the database, whenever I remove any record from the lead table I can't add another lead for the same customer because of the OneToOne relation.
When surfing through the internet, I got a few solutions for similar scenarios of unique constraints like using:
Partial indexes &
Virtual columns
But here I'm searching for some kind of TypeORM level solutions while mapping relations. What could be the best play around for this case?

One to One relationship in TypeORM creates a unique foreign key constraint by default. Though the row is soft-deleted from the Lead table, there will still be a unique value in the table. So, while inserting another lead for the same customer, TypeORM will throw us a unique constraint error.
The solution for this issue is to remove a foreign key constraint from the relationship. This will now allow us to insert data for the same customerId in the Lead table.
Now what we have to make sure of is:
before inserting value in the Lead table, check if there already exists another lead for that customerId that is not soft deleted.
We also have to ensure that before deleting any customer from the Customer table, their particular leads are soft-deleted from the Lead table.
P.S: In a way this solution is a hacky solution. But since soft delete doesn't consider the foreign key constraint references, this is the suitable way that I have found so far.

Related

Entity framework - referencing one of composite primary key columns as a foreign key

In table A I've got a composite of 3 columns as a primary key. I want to have only one of these three columns as a foreign key in table B, just to make sure that the value that I insert into table B's column exists in table A.
Currently from what I've read it looks like in Entity Framework I have to add all three columns of composite PK, which is not really what I need. The latest answer that I've found was of 2015, maybe since then something changed?
I know that I can add a manual check on each insert/update call, but I don't want to do that, maybe there is more elegant way.

Is it possible to use DBIx::Class on a database without relationships?

I'm new to DBIC. I've imported data into a database. It's not possible to create relationships between the tables because, apparently, not all the values in the child table's foreign key column have a corresponding value in the parent table.
So is it possible to still do joins between the tables? I've skimmed through the tutorial and documentation but found nothing that addresses this problem.
You can of course define relationships in your DBIC schema that don't have a matching constraint in the database.
If you use $schema->deploy it will automatically generate constraints for all foreign key columns.

EF many to many with junction entity database first

I have a junction table with and idenity primary key columns to realize a many to many relationship. Visual Studio automatically detects it as a many to many relationship and the junction table is not an entity.
How can i realize it that also this table is generated as an entity? I need this for breeze.js .
You just need to add additional columns (or properties) to that table (or model).
You said that your table has acolumn named ID and it's the primary key withe IsIdentity set to true. It must works, I'm using this approach...
There must be a problem or missing with your table definition. However, if all are OK, just add a nullable column in your table and update your model from database. The problem will go away.

EF db first and table without key

I am trying to use Entity Framework DB first to do quick prototyping of a reporting website for a huge db. The problem is one of the tables doesn't have a key. I got an 'Error 159: EntityType has no key defined'. If I add a key on the model designer, I got 'Error 3024: Must specify mapping for all key properties'. My question is whether there is a way to workaround this WITHOUT adding a key to the table. The table is not in our control.
Huge table which does not have a key? It would not be possible for you or for table owner to search for anything in this table without using full table scan. Also, it is basically impossible to use UPDATE by single row without having primary key.
You really have to either create synthetic key, or ask owner to do that. As a workaround, you might be able to find some existing column (or 2-3 columns) which is unique enough that it can be used as unique key. If it is unique but does not have actual index created, that would be still not good for performance - you should create such index.

Problem in mapping fragments in Entity Framework

I am using Entity Framework and I ran into an odd build error.
I am building a forum and I set up a table in the database for "ignores" when people don't like each other they will ignore someone. The table has two columns and together they are the primary keys.
PK InitiatingUser
PK IgnoredUser
When EF maps this table I get this error:
Error 7 Error 3034: Problem in mapping fragments starting at lines 1467, 1477:Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
I opened up the edmx in the XML editor and navigated to the offending lines.
<MappingFragment StoreEntitySet="Ignores">
<ScalarProperty Name="IgnoredUser" ColumnName="IgnoredUser" />
<ScalarProperty Name="InitiatingUser" ColumnName="InitiatingUser" />
</MappingFragment>
I am just getting started with EF and I don't understand what is going on or what the issue might be.
Edit
The relationships between ignores used to have foreign keys mapping both initiating user and ignored user to the primary key (username) of users table. That was how it was when I first mapped EF to this table. I have since deleted the FKs to see if that would help but it didn't.
This is likely due to including a many-to-many join table in your entity model, or what EF thinks is such a table (possibly such as one that doesn't have its own self-contained key, but whose identity is made up of two or more foreign keys).
So, for example, let's say you have the following tables:
Person
Address
PersonAddress (contains only PersonID and AddressID)
In your entity model, you should only add Person and Address. If you add PersonAddress, then EF will throw the error. According to this MSDN Q&A, EF will take the join table into account automatically.
I don't know what was wrong here, but I just deleted the table from the ORM and the DB then recreated it with an actual ID column, instead of two primary keys. I re-mapped the table, compiled, and all is well now. It would have been convenient to do it the way I had it, but oh well.
If anyone has any insight let me know. I'd rather accept someone else's answer.
PK InitiatingUser;
PK IgnoredUser
two primary key cannot allow edmx file.so create sno column in that table and make that as primary key . remove the pk of InitiatingUser and IgnoredUser.
now in that two column there is no primary key available.
like
Pk sno;
FK InitiatingUser;
FK IgnoredUser