Sequelize not creating join table many-to-many - many-to-many

When I start my server, soon after establishing a database connection I do this:
var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()
tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});
tool.sync();
requirement.sync();
I expect to see the join table being created, but it's not there. What am I missing?
Executing (default): CREATE TABLE IF NOT EXISTS `requirements` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): CREATE TABLE IF NOT EXISTS `tools` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `idCode` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`requirements`)
Executing (default): PRAGMA INDEX_LIST(`tools`)
It says in the documentation that the table is created for me. I shouldn't have to manually create one myself.
I am using:
Sequelize v3.23
SQLite 3

It seems instead of calling .sync() on every single model, I should just call sequelize.sync() once.
So that makes:
var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()
tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});
sequelize.sync();
//DO NOT tool.sync();
//DO NOT requirement.sync();
Here is an example of how I used it in my project: link

Related

error when forward engineering MySQL model

I have created a model in MySQL workbench, when I want to forward engineer it to create the "create" and "insert" script I get the following error:
ERROR:
Executing SQL script in server
ERROR: Error 1366: Incorrect integer value: 'G1' for column 'gebruiker_id' at row 1
SQL Code:
INSERT INTO `databaseher`.`gebruiker` (`gebruiker_id`, `voornaam`, `achternaam`, `E-mail`) VALUES ('G1', 'Ronny', 'Giezen', 'r.giezen#gmail.com')
I don't understand whats wrong with it, because the datatype of the column where the value "G1" inserts into is "VARCHAR(4)". It should be possible to insert both a letter and a number.... At least that's what I thought...
This is the create table:
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
If someone could help, that'll be awesome.
Thank you in advance!
Just a guess here because we don't have the full picture.
Can you run this:
Drop table 'databaseher'.'gebruiker'
And after recreate the table
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
rerun the insert.
I am guessing that the table was initially created with the column gebruiker as integer

Why does MySQL return 1175 code after calling UPDATE procedure WITH where clause which uses primary key?

I know that I can unchecked the 'safe update' mode but I want to understand why MySQL server returns this error even when all things look like correct?
I'm trying to call update procedure to update row in table country with statement
call sellcontroller.country_update(1, 'United Kingdom', 'GB', 'GBR', 'GBR');
but it throws an exception "Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column."
I'm definitely sure that I'm using a WHERE clause with table primary key and correct data type. And even more the same update statement used outside from procedure works correctly.
Code of the procedure is
CREATE PROCEDURE `country_update` (
in idCountry int(6),
in nameCountry varchar(255),
in codeISO2 varchar(2),
in codeISO3 varchar(3),
in codeCitizen varchar(5))
BEGIN
UPDATE `sellcontroller`.`country`
SET
`nameCountry` = nameCountry,
`codeISO2` = codeISO2,
`codeISO3` = codeISO3,
`codeCitizen` = codeCitizen
WHERE `idCountry` = idCountry;
END
The table creation statement for now is
CREATE TABLE `country` (
`idCountry` int(6) NOT NULL AUTO_INCREMENT,
`nameCountry` varchar(255) DEFAULT NULL,
`codeISO2` varchar(2) DEFAULT NULL,
`codeISO3` varchar(3) DEFAULT NULL',
`codeCitizen` varchar(5) DEFAULT NULL,
PRIMARY KEY (`idCountry`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

Creating a trigger that pulls information from another table in phpMyAdmin

I am trying to create a fairly complicated Trigger and I'm not sure if it can be done on phpMyAdmin.
Right now I have this query that creates a table with all the information I need from it.
CREATE TABLE SeniorDB_Shipping
SELECT
SeniorDB_Invoice.ID_Order,
SeniorDB_Customer.MCT_Code,
SeniorDB_Customer.Customer_Name,
SeniorDB_Customer.Customer_Address,
SeniorDB_Customer.Customer_City,
SeniorDB_Customer.Customer_State,
SeniorDB_Invoice.Shipping_Company
FROM SeniorDB_Customer
Join SeniorDB_Invoice ON SeniorDB_Customer.MCT_Code = SeniorDB_Invoice.MCT_Code
As you can see in the image, when I run the query, it pulls in information from the tables above the information. I'm trying (and failing) to create a trigger that will do this same thing without having to create a brand new table every single time. All the other posts I have seen are similar in regards to creating a table instead of inserting to a table.
What the trigger does is, when I enter the ID_Order, the rest of the information will get pulled from the database.
This is the trigger I have so far:
delimiter ~
create trigger SeniorDB_Shipping before insert on SeniorDB_Shipping
for each row begin
set new.SeniorDB_Shipping.MCT_Code = new.SeniorDB_Customer.MCT_Code,;
set new.SeniorDB_Shipping.Customer_Name = new.SeniorDB_Customer.Customer_Name,;
set new.SeniorDB_Shipping.Customer_Address = new.SeniorDB_Customer.Customer_Address,;
set new.SeniorDB_Shipping.Customer_City = new.SeniorDB_Customer.Customer_City,;
set new.SeniorDB_Shipping.Customer_State = new.SeniorDB_Customer.Customer_State,;
set new.SeniorDB_Shipping.Shipping_Company = new.SeniorDB_Customer.Shipping_Company,;
end~
delimiter ;
I feel like I'm right there. I just can't link it to when I enter the ID_Order.
This is the page if you would like to see the databases: http://polonium.forest.usf.edu/~sngamwon/SeniorProject/SeniorDB_Order.php
Ok, so you'll need to run this once:
/* Create the table with a primary key */
create table `SeniorDB_Shipping` (
`id` INT unsigned AUTO_INCREMENT NOT NULL,
primary key(id),
`ID_Order` int NOT NULL,
`MCT_Code` int NOT NULL,
`Customer_Name` varchar(255) NOT NULL,
`Customer_Address` varchar(255) NOT NULL,
`Customer_City` varchar(255) NOT NULL,
`Customer_State` varchar(255) NOT NULL,
`Shipping_Company` varchar(255) NOT NULL
) ENGINE=MyISAM CHARACTER SET=utf8 COLLATE utf8_general_ci;
Then you can run
/* Insert statement */
INSERT INTO `SeniorDB_Shipping` (
`ID_Order`,
`MCT_Code`,
`Customer_Name`,
`Customer_Address`,
`Customer_City`,
`Customer_State`,
`Shipping_Company`
) SELECT
invoice.ID_Order,
customer.MCT_Code,
customer.Customer_Name,
customer.Customer_Address,
customer.Customer_City,
customer.Customer_State,
invoice.Shipping_Company
FROM
SeniorDB_Customer as customer
Join SeniorDB_Invoice as invoice
ON customer.MCT_Code = invoice.MCT_Code;
I've run this in my own PHPMyAdmin, so it works. But I obviously don't have your schema. Known issues:
This will populate SeniorDB_Shipping with ALL the data from your two tables each time. Modify the query as required to select only recent data if that's not what you want. If ID_Order is a primary key you could check that doesn't already exist.

SQL Error #1007

I have this #1007 error with my SQL code. When I try to import into my database it gives me this #1007 error. The data base is called company. I'm new to SQL and it would be good if someone could help me out. Thanks
CREATE DATABASE company;
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
It looks like the database "company" may already exist. If you look at the link provided by #Marc B, the error "#1007" corresponds to:
"Error: 1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS)
Message: Can't create database '%s'; database exists
An attempt to create a database failed because the database already exists.
Drop the database first if you really want to replace an existing database, or add an IF NOT EXISTS clause to the CREATE DATABASE statement if to retain an existing database without having the statement produce an error."
Check your schema to make sure that you don't already have a database called "company" created.
Perhaps you are only trying to create the table "login", whereas you would simply need the code:
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
making sure you create this table in the already existing "company" database.
You are trying to create a database that has already been created.
Staring at your two commands, you could do CREATE TABLE IF NOT EXISTS
you also need to set default database before creating the table
CREATE DATABASE IF NOT EXISTS company;
USE company
CREATE TABLE login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);
or you could put the DB name before the table name
CREATE DATABASE IF NOT EXISTS company;
CREATE TABLE company.login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);

Stored Procedure for update not working

I am new to writing stored procedures, and i have some issues executing this one.it is able to update the name and address but is not able to update the employeecount.And i am not able to track the error i have made.
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_update_company_details`(
IN p_id int(11),
IN p_name varchar(45),
IN p_address varchar(45),
IN p_num_employee int(11)
)
BEGIN
UPDATE company
SET name=p_name,
address=p_address,
employeecount=p_num_employee
WHERE id=p_id;
END
And this the table in the database
CREATE TABLE `company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`address` varchar(45) DEFAULT NULL,
`employeecount` int(11) DEFAULT NULL,
`logo` blob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
A std table,not anything fancy.
And this is how iam calling this stored procedure.
MySqlCommand cmd = new MySqlCommand(String.Format("call sp_update_company_details({0},'{1}','{2}',{3})",
id,request.Name,request.Address,request.NumberOfEmployees), conn);
id and request are passed as parameter to the function where this call is made.
This is almost certainly an issue with your calling code not the stored procedure. If you try executing it from workbench, it should succeed without any issues.
To clean-up and fix your code, you really should make your call (all of your calls and queries) paramaterized like so:
using (var conn = new MySqlConnection("connString"))
{
using (var cmd = new MySqlCommand("sp_update_company_details"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("p_id", id);
cmd.Parameters.AddWithValue("p_name", request.Name);
cmd.Parameters.AddWithValue("p_address", request.Address);
cmd.Parameters.AddWithValue("p_num_employee", request.NumberOfEmployees);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
I would also do a step through to ensure that the values you think are being passed by request are actually the values you think they are.