Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
Crear tabla con error de sintaxis de clave externa

Estoy tratando de hacer 3 tablas usando consultas sql en node.js

las primeras 2 consultas funcionan bien y crean las tablas. pero el ultimo tiene un error de sintaxis pero no se que es lo que esta mal...

 connection.connect(function(err) { if (err) throw err; console.log("Connected!"); const queryUserMgmtTable = "CREATE TABLE if not exists user_mgmt (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), surname VARCHAR(50), email VARCHAR(50), birth_date DATETIME)"; connection.query(queryUserMgmtTable, function (err, result) { if (err) throw err; console.log("Table user_mgmt created"); }); const queryTaskMgmtTable = "Create table if not exists task_mgmt(id int Primary key NOT NULL, name Varchar(50), Result ENUM('created', 'in_progress', 'done') NOT NULL)"; connection.query(queryTaskMgmtTable, function (err, result) { if (err) throw err; console.log("Table task_mgmt created"); }); // something is wrong with the foreign key here: const queryUser_taskTable = "Create table if not exists user_task(UserId int FOREIGN KEY REFERENCES user_mgmt(id), UserId int FOREIGN KEY REFERENCES task_mgmt(id))"; connection.query(queryUser_taskTable, function (err, result) { if (err) throw err; console.log("Table user_task created"); }); });

El error es el siguiente:

sqlMessage: "Tiene un error en su sintaxis SQL; consulte el manual que corresponde a la versión de su servidor MySQL para conocer la sintaxis correcta para usar cerca de 'REFERENCIAS DE CLAVE EXTERNAS' user_mgmt (id)', UserId int REFERENCIAS DE CLAVE EXTERNAS 'tarea_' en la línea 1",

sql: "Crear tabla si no existe user_task (UserId int REFERENCIAS DE CLAVE EXTERNAS 'user_mgmt (id)', UserId int REFERENCIAS DE CLAVE EXTERNAS 'task_mgmt (id)')"

Realmente no sé qué está mal allí, espero que me puedas ayudar con eso.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Diría que tiene un error tipográfico ya que está creando una tabla con 2 columnas con el mismo nombre y, por lo tanto, lo escribiré en la forma en que creo que debería ser. La sintaxis de las claves foráneas para MYSQL debe estar al final de la definición de la tabla (al igual que otros índices, pero podría no ser cierto para las versiones más nuevas)

 Create table if not exists user_task( UserId int, TaskId int, FOREIGN KEY (UserId) REFERENCES user_mgmt(id), FOREIGN KEY (TaskId) REFERENCES task_mgmt(id))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!