finalmente logré hacer esto (como por mi comentario) pero con el fin de hacer así que tuve que escribir código. Al final creé algunas tablas ficticias que realizaban un seguimiento de la antigua identificación contra nueva identificación. Al copiar registros con restricciones FK, busqué la nueva identificación basada en la anterior. Un poco largo sin aliento, pero funcionó.
Esta publicación está empezando ahora, así que la he marcado como la respuesta. Si alguien tiene mejores ideas/soluciones que funcionen, felizmente lo desmarcaré como la respuesta aceptada.
EDITAR: Según lo solicitado aquí hay algún pseudo-código que espero explique cómo lo hice.
que tienen las dos tablas relacionadas de la siguiente manera:
CREATE TABLE tblCustomers (
Id int NOT NULL AUTO_INCREMENT,
Name varchar(50) DEFAULT NULL,
Address varchar(255) DEFAULT NULL,
PRIMARY KEY (Id)
)
ENGINE = MYISAM
ROW_FORMAT = fixed;
CREATE TABLE tblQuotes (
Id int NOT NULL AUTO_INCREMENT,
CustomerId int(11) DEFAULT NULL,
QuoteReference varchar(50) DEFAULT NULL,
PRIMARY KEY (Id)
)
ENGINE = MYISAM
ROW_FORMAT = fixed;
puedo crear una tabla adicional que voy a utilizar para realizar un seguimiento de los identificadores de viejos contra los nuevos identificadores de
CREATE TABLE tblLookupId (
Id int NOT NULL AUTO_INCREMENT,
TableName varchar(50) DEFAULT NULL,
OldId int DEFAULT NULL,
NewId int DEFAULT NULL,
PRIMARY KEY (Id)
)
ENGINE = MYISAM
ROW_FORMAT = fixed;
La idea es que copio el tblCustomer filas uno a la vez y realizar un seguimiento de los identificadores como voy, así:
// copy each customer row from dev to live and track each old and new id
//
foreach (customer in tblCustomers)
{
// track the old id
var oldid = customer.id; // e.g. 1
// insert the new record into the target database
INSERT newdb.tblCustomers (...) VALUES (...);
// get the new id
var newid = SELECT LAST_INSERT_ID() // e.g. 245
// insert the old id and the new id in the id lookup table
INSERT idlookup (TableName, OldId, NewId) VALUES ('tblCustomers', oldid, newid); // this maps 1->245 for tblCustomers
}
Cuando voy a copiar el t able (tblQuote) con la clave externa Tengo que buscar primero la nueva identificación basada en la anterior.
// copy each quote row from dev to live and lookup the foreign key (customer) from the lookup table
//
foreach(quote in tblQuotes)
{
// get the old foreign key value
var oldcustomerid = quote.CustomerId; // e.g 1
// lookup the new value
var newcustomerid = SELECT newid FROM tblIdLookup WHERE TableName='tblCustomers' AND oldid=oldcustomerid; // returns 245
// insert the quote record
INSERT tblQuotes (CustomerId, ...) VALUES (newcustomerid, ...);
}
He tratado de mantener este corto y al punto (y el idioma agnóstico) lo que la técnica puede ser visto. En mi situación real, tenía alrededor de 15 tablas en cascada, así que tuve que rastrear los nuevos identificadores de cada tabla no solo tblCustomer
¿Podría ser más específico sobre lo que hizo para resolver esto? Sé que ha pasado un tiempo, pero esta es una gran pregunta, y una respuesta completa sería genial. – kittykittybangbang
@kittykittybangbang He agregado un pseudocódigo que espero explique esta técnica. Lo he hecho varias veces, no es rápido ni bonito, pero funciona como un hechizo. –
¡Muchas gracias! :RE – kittykittybangbang