Skip to content

Conversation

@macbre
Copy link
Contributor

@macbre macbre commented Sep 22, 2016

We have tables that have order column, let's wrap column names in backticks when performing INSERT query.

INSERT IGNORE INTO vpt_asset(asset_id, updated_by, section, updated_at, program_id, data, order) VALUES (%(asset_id)s, %(updated_by)s, %(section)s, %(updated_at)s, %(program_id)s, %(data)s, %(order)s);
CREATE TABLE `vpt_asset` ( `asset_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `program_id` int(10) unsigned NOT NULL, `section` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '', `order` tinyint(4) NOT NULL DEFAULT '1', `data` blob NOT NULL, `updated_by` int(10) unsigned NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`asset_id`), UNIQUE KEY `asset` (`program_id`,`section`,`order`), KEY `program_id` (`program_id`,`section`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

@wladekb

ignore = 'IGNORE '

sql = 'INSERT {}INTO {}({}) VALUES ({});'.format(ignore, table, ', '.join(columns), ', '.join(values))
sql = 'INSERT {}INTO {}(`{}`) VALUES ({});'.format(ignore, table, '`, `'.join(columns), ', '.join(values))
Copy link

@wladekb wladekb Sep 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather

map(lambda s: '`{}`'.format(s), columns) 

so that the backticks stay in one place and the code is more readable overall.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the implementation in 9bf2479

@macbre macbre force-pushed the SqlBuilderMixin-columns-handling branch from ed09aa2 to 9bf2479 Compare September 23, 2016 07:40
@macbre macbre merged commit 258beb6 into master Sep 23, 2016
@macbre macbre deleted the SqlBuilderMixin-columns-handling branch September 23, 2016 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants