SQL migrations. Keeping old ID is not working - mysql

I'm migrating an old database to a new database (mysql => postgres).
For simplicity i kept the old IDs so id did something like that in knex
knex("my_table").withSchema("mySchema").insert({
id : old[i].id,
info : old[i].info
})
It appears doing that cancels postgres auto_inc on ID and thus when i try to insert later like that :
knex("my_table").withSchema("mySchema").insert({
info : "information"
})
It will automaticly attribute it to ID : 1. Even if it already exist because of the migration.
My question is : Can i still keep the old id ?

please try altering the sequence assigned to the my_table.id column for auto-increment.
alter it such that it will start from max(old_id)+1
http://www.postgresqltutorial.com/postgresql-serial/
https://www.postgresql.org/docs/9.1/static/sql-altersequence.html

Related

Delete many2many column in table and the table itself - odoo 13

While running a pre-migration script to delete a (wizard) transient model, ended up with below mentioned issue.
from openupgradelib import openupgrade
#openupgrade.migrate()
def migrate(env, version):
openupgrade.delete_records_safely_by_xml_id(
env,
["moduel_name.view_id)"],
delete_childs=True,
)
try:
env.cr.execute("DROP TABLE IF EXISTS table_name CASCADE")
env.cr.execute("DROP TABLE IF EXISTS dependent_table_names CASCADE")
except Exception as e:
raise ("Exception--------------", e)
Error:
psycopg2.errors.ForeignKeyViolation: update or delete on table "ir_model" violates foreign key constraint "ir_model_relation_model_fkey" on table "ir_model_relation"
Similar issue: https://github.com/odoo/odoo/issues/54178
According to the above issue, having Many2many in transient model might cause this issue. It is true in my case as well. I have many2many fields. No solution there.
I kind of tried deleting the problematic fields(Many2many) before deleting columns. But it is known that many2many fields can't be located in db. kind of stuck.
openupgrade.drop_columns(
env.cr,
[
("table_name", "any_other_column_name"), # ---> This works
("table_name", "many2many_column_name"), # ---> This doesn't
],
)
is there anyway to get rid of many2many fields from the model ? Any help is appreciated.
Could you try this :
Let's say your Transient is my_transient_model and the Many2many field is e.g. sale_line_ids = fields.Many2many('sale.order_line')
First thing to know : Did you specify the relation table ? like
sale_line_ids = fields.Many2many('sale.order_line', 'my_relation_table_name') ?
If so, 'my_relation_table_name' is the name you want to delete from ir_model_relation.
If not, the relation table name is my_transient_model_sale_order_line_rel (so model then _ then the model we point to with _ instead of . then _rel.
Second set: delete the data from ir_model_relation:
DELETE FROM ir_model_relation WHERE name='my_transient_model_sale_order_line_rel';
Then you should be able to delete the Many2many table :
DROP TABLE my_transient_model_sale_order_line_rel;
(for sure, change my_transient_model_sale_order_line_rel if you specified the relation table like my_relation_table_name in the example)
Hope it helped, keep me updated :)

Are there any way to create singular-name DB table?

development environment
Lnaguage : Golang ver.1.9.2
DB : mySQL
Framework : not decided (Maybe I'll use revel)
situation
I already have DB which has singular-name table ,like "user", "page". It can't be changed.
Now I'll develop new application using this DB.
I created simple application to connect this DB, and tried to auto migrate using gorm(https://github.com/jinzhu/gorm).
I defined some models, like "user" which is same as existing DB table name, and run auto-migrate just as it written in (http://jinzhu.me/gorm/database.html#connecting-to-a-database )
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
Then, new table "users" was created.
Question
Can I create singular-name table, like "user" with auto-migrate or those things ?
Using gorm is not required, so I'll use another orm library if it works.
I hope anyone help me !
Implement function TableName on struct User to return a custom name for the table. The ORM uses this function to get the table name for all DB operations.
func (user *User) TableName() string {
return "user"
}
Refer docs here: http://jinzhu.me/gorm/models.html#table-name-is-the-pluralized-version-of-struct-name
You have set with db instance to use singular table, like this:
db.SingularTable(true)

FindBy columnName when column name contains "Id"

In Grails, Gorm, I have this entity:
class MyEntity implements Serializable {
Long bankTransactionId
int version
BigDecimal someValue
static constraints = {
bankTransactionId(nullable: false)
version(nullable: true)
someValue(nullable: true)
}
}
Doing MyEntity.findByBankTransactionId(Long.valueOf("3")) throws this exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 'this_.id' in 'field list'
I am suspecting the fact that my column has the name id in it. Could it be this?
How to fix it then ?
Thanks.
Everything you have provided here looks fine. In particular, there are no restrictions about having the letters "id" in a column name.
Take a look at your generated MySQL table. I'm guessing that the id column isn't there for some reason. Maybe something prevented generating it due to some earlier error that you have now corrected, or you have your datasource set to "none" instead of "update" (or similar) and the whole table is missing!
If this is just a development environment with no real data (and no foreign key constraints), drop the whole MyEntity table and let it be automatically recreated. If not, move to a different temporary datasource, let a new table be created, and compare the two. If the new one still doesn't have an id column, you have something going wrong during your startup that is preventing your tables from being created correctly. You could just add the column in manually, but if you don't figure out what happened to it in the first place, it will probably just happen again.
For reference, in my test environment, my MySQL table for "MyEntity" copied from your example looks like:
desc my_entity;
'id','bigint(20)','NO','PRI',NULL,'auto_increment'
'version','int(11)','YES','',NULL,''
'bank_transaction_id','bigint(20)','NO','',NULL,''
'some_value','decimal(19,2)','YES','',NULL,''

Talend: Create table if does not exist, update it if not does not work

I want to create a job in Talend Open Studio where I can insert data if data does not already exist and update rows if the row already exists.
I tried to use a tMap with two outputs:
output1 : fill data into a tMysqlOutput with :
action on table : default (no operation is carried out)
action on data : insert
output2 : update data using another tMysqlOutput with :
action on table : default (no operation is carried out)
action on data : update
But I want it to update only the modified rows.
How can I do that?
EDITED :
The following table is an example that could explain more my problem.
I want the following result :
if the name changed so I have the current date in the "updated_on" field
if the name does not change (there is no modified data) the "updated_on" field does not change

How to correctly define one-to-many relations with existing joint table using node Sequelize

I am using Sequelize, but since I also have other servers running other than node.js, I need to let them share the database. So when defining one-to-many relation, I need to let Sequelize use the old existing jointTable.
I write my definition of the association as below, where pid is the primary key of presentations:
this.courses.hasMany(this.presentations,
{as : 'Presentations',
foreignKey : 'cid',
joinTableName : 'course_presentations'
});
Or this one:
this.courses.hasMany(this.presentations,
{as : 'Presentations',
foreignKey : 'pid',
joinTableName : 'course_presentations'
});
I am using the below codes to retrieve the associated presentations:
app.get('/courses/presentations/:cid', function (req, res){
var cid = req.params.cid;
app.models.courses.find({where: {cid:cid}}).success(function(course){
course.getPresentations().success(function(presentations){
res.send(presentations);
});
});
});
The previous one will tell me there is no cid in 'presentations' table.
The latter one will give something like this:
Executing: SELECT * FROM `courses`;
Executing: SELECT * FROM `courses` WHERE `courses`.`cid`='11' LIMIT 1;
Executing: SELECT * FROM `presentations` WHERE `presentations`.`pid`=11;
Check carefully, I found that everytime, it is always using the cid value to query for presentations, which means only when they happen to share the same id value, something can be returned. And even for those, it is not correct.
I am strongly suspecting, Sequelize are not using the joinTable I specified, instead, it is still trying to find the foreign keys in the original two tables. It is viewing pid as the reference of cid in presentations, which causes this problem.
So I am wondering how to correctly set up the junction table so that the two of them can use the foreign keys correctly.
jointTableName : 'course_presentations'
should be (without a t)
joinTableName : 'course_presentations'
Actually AFAIK - this kind of relation is not "pure" one-to-many.
You have one course can have many entries in course_presenation table, and course_presentation have one-to-one relation with presentation. If so, just define that kind of associations in model.