简体   繁体   中英

Deleting multiple rows based on where clause using FluentMigrator

Given a Payment table, a PaymentStatus table, and a PaymentHistory table, I need to delete a status from the PaymentStatus table that the Payment and PaymentHistory tables have associated records for. So naturally that means delete the history table before the payment table. Of course the historical data has multiple records of payments in different statuses. What I need is a where clause for the history table delete... the sql would look kind of like this:

DELETE FROM PaymentHistory 
WHERE PaymentId IN 
(SELECT Id FROM Payment WHERE PaymentStatusCode = 'Processing')

What I don't understand is how to achieve this in FluentMigrator. This is what I have so far but there are no .Where() or other extension methods available after the .FromTable()

Delete.FromTable("PaymentHistory").Row(new {PaymentStatusCode = "Processing"});

Thanks in advance for the help.

Now I feel stupid for asking....

const string cascadeDeleteScript = 
@"DELETE FROM PaymentHistory WHERE PaymentId IN 
              (SELECT Id FROM Payment WHERE PaymentStatusCode = 'Processing') ";
Execute.Sql(cascadeDeleteScript);

How about setting the table up to Cascade Delete?

How to add 'ON DELETE CASCADE' in ALTER TABLE statement

It seems like you would have to remove the Primary-Foreign Key constraint and recreate it with the Cascade in mind, but then any time you delete from Payment you will also delete from PaymentHistory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM