I want to stop sequelize to drop tables this is making difficult for me adding dummy data again again whenever I restart server.
server running on port 3000
Executing (default): DROP TABLE IF EXISTS `teams`;
Executing (default): DROP TABLE IF EXISTS `teams`;
Executing (default): CREATE TABLE IF NOT EXISTS `teams` (`teamId` INTEGER auto_increment , `teamName` VARCHAR(255), `teamEmail` VARCHAR(255), `college` VARCHAR(255), `isPaid` TINYINT(1) NOT NULL DEFAULT false, `isRone` TINYINT(1) NOT NULL DEFAULT false, `isRtwo` TINYINT(1) NOT NULL DEFAULT false, `isRthree` TINYINT(1) NOT NULL DEFAULT false, `isNIT` TINYINT(1) NOT NULL DEFAULT false, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`teamId`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `teams` FROM `startupconclave`
Nice! Database looks fine
Remove {force: true} from your bin/www script in models.sequelize.sync() method.
Related
I am making an App where there are message boards and for each board, there is a number of threads and for each thread, there is a number of replies
I need to make a query that gets the threads of a specific board (by board_id) and within each thread, an array of it's replies if no replies for the thread then, get it with an empty replies array
I am not sure if it can be done by a pg query, and it might be achieved by a simple code in the backend modifying the data coming from the query to the format I like but I find a solution currently
here are the tables:
CREATE TABLE threads (
_id SERIAL NOT NULL PRIMARY KEY,
board_id INT NOT NULL REFERENCES board(id),
text VARCHAR(255) NOT NULL,
delete_password VARCHAR(255) NOT NULL,
reported BOOLEAN NOT NULL,
created_on DATE NOT NULL DEFAULT CURRENT_DATE,
bumped_on DATE DEFAULT CURRENT_DATE
);
CREATE TABLE board (
_id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL
);
CREATE TABLE replies (
_id SERIAL PRIMARY KEY NOT NULL,
thread_id INT NOT NULL REFERENCES threads(id),
text VARCHAR(255) NOT NULL,
delete_password VARCHAR(255) NOT NULL,
reported BOOLEAN NOT NULL,
created_on DATE NOT NULL DEFAULT CURRENT_DATE
);
Here is my model ,
var traders = sequelize.define('traders', {
.....
}, {});
it has many to many self association
traders.belongsToMany(models.traders,{
as:'feedbackClient',
through:'feedback'
});
idea is one trader can give feedback to other trader on each successful trade.
but when i sync it generates table with this SQL query
Executing (default): CREATE TABLE IF NOT EXISTS "feedbacks" ("id" S`ERIAL , "rating" "public"."enum_feedbacks_rating", "comment" VARCHAR(255), "traderId" INTEGER REFERENCES "traders" ("id") ON DELETE CASCADE ON UPDATE CASCADE, "feedbackClientId" INTEGER REFERENCES "traders" ("id") ON DELETE CASCADE ON UPDATE CASCADE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, UNIQUE ("traderId", "feedbackClientId"), PRIMARY KEY ("id"));`
how can i remove this constraint?
UNIQUE ("traderId", "feedbackClientId")
so that i can add multiple records with same combination of traderId and feedbackClientId.
Got a solution here, please post your answers if you have better solutions.
Scenario: I am dealing with a database which is running both postgres and infobright, where infobright is set to default engine.
Goal: Have Sequelize create a table in postgres
Problem: Sequelize is creating table in Infobright by default.
Detailed Explanation:
So when I am trying to define a model which look like:
sequelize.define('mymodel', {
file_name: {
type: Sequelize.STRING,
}
}, {
schema: 'myschema'
});
The sql that get generated looks like:
create
table
if not exists "myschema"."mymodel"(
"id" serial,
"file_name" varchar(255),
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone not null,
primary key("id")
);
NOTE: Because with (engine = postgres) is missing, the system tries to create table in the default engine i.e. infobright, which is not what I expect it to do.
Expected SQL
create
table
if not exists "myschema"."mymodel"(
"id" serial,
"file_name" varchar(255),
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone not null,
primary key("id")
) with (
engine = postgres
);
Note: Executing the above generates a table in postgres.
I tried specifying engine in sequelize as well as model configuration, but it doesn't work.
Questions: How shall I configure Sequelize or my model so that it generates the expected sql. Open to other suggestions as well.
This the schema design from my mysql table, I am trying to migrate to mongodb and I am using mongoose , I am confuse of the datatype to use in creating model for my locators table or collection... I am still new on this mongodb hope you can help me. or is it okay in mongoose to use model without declaring datatype ?
like this
var locatorModel = new Schema({});
Thank you in advance.
CREATE TABLE `locators` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`mch_id` VARCHAR(15) NOT NULL ,
`lat` DOUBLE(10,6) NULL DEFAULT NULL,
`lng` DOUBLE(10,6) NULL DEFAULT NULL,
`latcot` CHAR(1) NULL DEFAULT NULL,
`ttime` TIME NULL DEFAULT NULL,
`tdate` DATE NULL DEFAULT NULL,
`tdatetime` DATETIME NULL DEFAULT NULL,
`velo` DECIMAL(5,2) NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `locators_mch_id_tdatetime_index` (`mch_id`, `tdatetime`),
INDEX `locators_created_at_index` (`created_at`)
)
I'm developing an aplication using nodejs + sequelize. I read the documentation of sequelize (which isn't great...) and since I'm in development environment I want to drop the tables before a new execution, so, this is the code
sequelize.sync({
force: true,
logging: console.log
}).then(function(){});
If there are no tables in the database it produces an exception, which I think it should not
Executing (default): DROP TABLE IF EXISTS "vouchers" CASCADE;
Unhandled rejection SequelizeDatabaseError: must be owner of relation vouchers
at Query.formatError
If I already had the tables, then it tries to drop the last table twice, which repeats the exception
Executing (default): DROP TABLE IF EXISTS "vouchers" CASCADE;
Executing (default): DROP TABLE IF EXISTS "requestline" CASCADE;
Executing (default): DROP TABLE IF EXISTS "requests" CASCADE;
Executing (default): DROP TABLE IF EXISTS "products" CASCADE;
Executing (default): DROP TABLE IF EXISTS "blacklists" CASCADE;
Executing (default): DROP TABLE IF EXISTS "costumer" CASCADE;
Executing (default): DROP TABLE IF EXISTS "costumer" CASCADE;
Unhandled rejection SequelizeDatabaseError: must be owner of relation
costumer
at Query.formatError
Can't understand why...
So, I worked around it like this
//workaround
sequelize.sync({
force: true,
logging: console.log
})
.then(function () { })
.catch(function (err) {
sequelize.sync({
logging: console.log
}).then(function () { })
})
Which indeed works, but it's ugly...
Any thoughts on this?