简体   繁体   English

在sequelize中向现有表添加新列

[英]adding new column to the existing table in sequelize

I have a table in the database and now I want to add two new columns to it. 我在数据库中有一个表,现在我想向其中添加两个新列。 I have altered the Table using the MYSQL cmd line and also added the newly added columns into the Sequelize model. 我使用MYSQL cmd line更改了表,还将新添加的列添加到了Sequelize模型中。

But when I fire the insert query, it still fires the query with previous fields and does not adds up the newly added columns to it. 但是,当我触发插入查询时,它仍然会触发具有先前字段的查询,并且不会将新添加的列加到其中。

Here is my data Model: 这是我的数据模型:

var Observation = sequelize.define('observation_details', {
  obv_id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  obv_source: Sequelize.STRING,
  obv_waveband: Sequelize.INTEGER,
  prpsl_id: Sequelize.INTEGER,
  obv_ra: Sequelize.DOUBLE,              //newly added column
  obv_dec: Sequelize.DOUBLE              // newly added column
}, {
  tableName: 'observation_details',             
  timestamps: false                             
});

这就是数据库中数据库的外观

Here in the table obv_ra and obv_dec are already added..! 在此表中,已经添加了obv_raobv_dec

This is the Insert Query: 这是插入查询:

Observation.create({
  obv_source: req.body.obvDetails[i].source,
  obv_waveband: req.body.obvDetails[i].waveband,
  prpsl_id: proposalId,
  obv_ra: req.body.obvDetails[i].ra,
  obv_dec: req.body.obvDetails[i].dec
})

adding Sequelize.sync({force:false}) results in : 添加Sequelize.sync({force:false})导致:

Executing (default): CREATE TABLE IF NOT EXISTS observation_details (obv_id INTEGER , obv_source VARCHAR(255), obv_waveband INTEGER, prpsl_id INTEGER, obv_ra DOUBLE PRECISION, obv_dec DOUBLE PRECISION, PRIMARY KEY (obv_id)) ENGINE=InnoDB; 执行(默认):如果表不存在observation_details,则创建表(obv_id INTEGER,obv_source VARCHAR(255),obv_waveband INTEGER,prpsl_id INTEGER,obv_ra DOUBLE PRECISION,obv_dec DOUBLE PRECISION,PRINARY KEY(obv = idIn)DB)

but still the query fired is : 但仍然触发的查询是:

Executing (default): INSERT INTO observation_details (obv_id,obv_source,obv_waveband,prpsl_id) VALUES (NULL,dgf,2,19); 执行(默认):插入INTO观察细节(obv_id,obv_source,obv_waveband,prpsl_id)值(NULL,dgf,2,19);

Is there any way to work this out without using Migrations ? 有没有办法在不使用Migrations情况下解决此Migrations

I suppose there is if you're willing to lose all data, you can delete and start from scratch again. 我想如果您愿意丢失所有数据,则可以删除并重新开始。

You can also do some fancy logic before the create to check the schema of the table on disk vs what you expect it to be from the model and add/remove columns as appropriate, but at that point you're just reimplementing a less flexible version of Migrations . 您还可以在创建之前做一些花哨的逻辑,以检查磁盘上的表的模式与模型中的期望值,并根据需要添加/删除列,但是此时您只是在重新实现一个不太灵活的版本Migrations Unless there's a compelling reason not to, you should use Migrations . 除非有令人信服的理由,否则应使用Migrations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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