我有一个 EXISTING 表,它有一个名为 ID 的主键和 6 个与发票相关的其他字段。我需要从旧表中插入值并将所有值插入到新的但最近创建的表中。旧表列出了发票编号,有时发票编号重复。我需要我正在尝试创建的这个新列,称为 invoice_id 到 AUTO_INCREMENT 当没有为将要插入的 future 值插入任何值时,并允许对现有值和 future 值进行重复。当没有插入值时,需要auto_increment。
ID (primary) || invoice_ID (needs to auto_increment AND allow duplicates) || other colums
1 || 1
2 || 2
3 || 2
4 || 3
我已经尝试了一些命令,结果是这样的:
ALTER TABLE `invoices` ADD `invoice_ID` INT NOT NULL AUTO_INCREMENT AFTER `ID` ,
ADD PRIMARY KEY ( `facture` )
结果:
MySQL said:
#1075 - Incorrect table definition; there can be only one auto column and it must be
defined as a key
还尝试过:
ALTER TABLE `invoices` ADD `invoice_ID` INT NOT NULL AUTO_INCREMENT AFTER `ID` ,
ADD KEY ( `invoice_ID` ) ,
ADD INDEX ( `invoice_ID` )
结果:
#1075 - Incorrect table definition; **there can be only one auto column** and it must
be defined as a key
我也尝试了一些不同的选项,比如当然不添加为主键,但似乎只要我添加了 auto_increment 请求,它就会使我的查询“作为主键”。
最佳答案
您可以使用触发器来完成。这是一个例子。
所以你有你的旧表:
drop table if exists invoices_old;
create table invoices_old (
invoice_ID int,
another_column int
);
insert into invoices_old values
(1,11),
(2,12),
(2,13),
(3,14),
(4,15),
(5,16),
(6,17),
(6,18),
(7,19);
你想插入到你的新表中:
drop table if exists invoices_new;
create table invoices_new (
id int not null auto_increment,
invoice_ID int default null, /*it's important here to have a default value*/
another_column int,
primary key (id)
);
你可能像这样复制你的数据:
insert into invoices_new (invoice_ID, another_column)
select invoice_ID, another_column
from invoices_old;
现在您的数据已在新表中,您可以在新表上创建触发器来模拟 auto_increment 列。
drop trigger if exists second_auto_inc;
delimiter $$
create trigger second_auto_inc before insert on invoices_new
for each row
begin
set @my_auto_inc := NULL;
select max(invoice_ID) into @my_auto_inc from invoices_new;
set new.invoice_ID = @my_auto_inc + 1;
end $$
delimiter ;
现在,当您向新表中插入更多行时
insert into invoices_new (another_column)
select 20 union all select 21 union all select 22;
看看你的 table
select * from invoices_new;
有效。
结果:
id invoice_ID another_column
1 1 11
2 2 12
3 2 13
4 3 14
5 4 15
6 5 16
7 6 17
8 6 18
9 7 19
16 8 20
17 9 21
18 10 22
您可能想知道为什么在真正的 auto_increment 列中 ID 从 9 跳到 16。最近在 SO 上有一篇关于它的好帖子,但我现在找不到它。无论如何,这不是你需要担心的。 Auto_increment 用于确保唯一性,而不是无缝序列。
关于mysql - 添加第二个自增字段并允许重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793020/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司