Hello I want to create a table with ID column but dont want auto increment used. (columns in table are id and string) I am using mysql. Can you help me write the create migration? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2009/11/28 Ritvvij <ritvij.j-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Hello > > I want to create a table with ID column but dont want auto increment > used. (columns in table are id and string) I am using mysql. Can you > help me write the create migration?Why do you not want auto increment? If it is because you want control of an id field for something like a product code or user number then it is probably easier to let Rails use the normal auto increment id field and add another one of your own, product_code or whatever it is, that you can manipulate as you wish without having to go against the Rails conventions. This is more likely to give you a peaceful life. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yup thought of what you said already but dont like it because already my app has become huge: 1. almost 14 reference tables and 10 app tables. 2. 4 -5 deployments Every time I change some reference table with global impacts. I dont want a new id because other application tables across all the deployments might be using that particular reference table id. So I want to fix up my reference table ids permanently by switching off auto increment and taking control over it. Of course app tables i will keep auto increment on. This way any ref table change, I will write 1 migration to delete all refs tables and rewrite them ... increasing the deployment window but simplifying how to steps. On Nov 28, 9:51 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/11/28 Ritvvij <ritvi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > Hello > > > I want to create a table with ID column but dont want auto increment > > used. (columns in table are id and string) I am using mysql. Can you > > help me write the create migration? > > Why do you not want auto increment? If it is because you want control > of an id field for something like a product code or user number then > it is probably easier to let Rails use the normal auto increment id > field and add another one of your own, product_code or whatever it is, > that you can manipulate as you wish without having to go against the > Rails conventions. This is more likely to give you a peaceful life. > > Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2009/11/28 Ritvvij <ritvij.j-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Yup thought of what you said already but dont like it because already > my app has become huge: > 1. almost 14 reference tables and 10 app tables. > 2. 4 -5 deploymentsDo you mean that the table is an existing table in a legacy db? If so then google rails legacy and you will find how to use an existing field as the id. Even if it is not an existing table then sitll google it as you will find answers to your problem there.> > Every time I change some reference table with global impacts. I dont > want a new id because other application tables across all the > deployments might be using that particular reference table id. So I > want to fix up my reference table ids permanently by switching off > auto increment and taking control over it.Do you mean you will completely rewrite the table using a migration or similar, but wish to set specific id values? I believe in this case it is possible to force id fields to particular values even though it is nominally auto increment. Though I have not done it myself. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 28, 2009, at 11:42 AM, Colin Law wrote:> 2009/11/28 Ritvvij <ritvij.j-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> Yup thought of what you said already but dont like it because already >> my app has become huge: >> 1. almost 14 reference tables and 10 app tables. >> 2. 4 -5 deployments > > Do you mean that the table is an existing table in a legacy db? If so > then google rails legacy and you will find how to use an existing > field as the id. Even if it is not an existing table then sitll > google it as you will find answers to your problem there. > >> >> Every time I change some reference table with global impacts. I dont >> want a new id because other application tables across all the >> deployments might be using that particular reference table id. So I >> want to fix up my reference table ids permanently by switching off >> auto increment and taking control over it. > > Do you mean you will completely rewrite the table using a migration or > similar, but wish to set specific id values? I believe in this case > it is possible to force id fields to particular values even though it > is nominally auto increment. Though I have not done it myself. > > Colin > > --If you provide an id for a model, that id will be used. ref = RefModel.new(fields) {|r| r.id = 42 } ref.save You can''t assign the id in the attributes hash of the .new method because ActiveRecord automatically protects the id from mass- assignment. If you set the id in a block or in a separate statement, then it will be used (unless it causes a conflict at the database level because, for example, it has a unique index). -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
lancecarlson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Nov-30 08:58 UTC
Re: Disable autoincrement
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001901 create_table(:suppliers, :id => false) do |t| t.column :name, :string, :limit => 60 # Other fields here end :id => false is the key here On Nov 28, 12:29 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Nov 28, 2009, at 11:42 AM, Colin Law wrote: > > > > > > > 2009/11/28 Ritvvij <ritvi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >> Yup thought of what you said already but dont like it because already > >> my app has become huge: > >> 1. almost 14 reference tables and 10 app tables. > >> 2. 4 -5 deployments > > > Do you mean that the table is an existing table in a legacy db? If so > > then google rails legacy and you will find how to use an existing > > field as the id. Even if it is not an existing table then sitll > > google it as you will find answers to your problem there. > > >> Every time I change some reference table with global impacts. I dont > >> want a new id because other application tables across all the > >> deployments might be using that particular reference table id. So I > >> want to fix up my reference table ids permanently by switching off > >> auto increment and taking control over it. > > > Do you mean you will completely rewrite the table using a migration or > > similar, but wish to set specific id values? I believe in this case > > it is possible to force id fields to particular values even though it > > is nominally auto increment. Though I have not done it myself. > > > Colin > > > -- > > If you provide an id for a model, that id will be used. > > ref = RefModel.new(fields) {|r| r.id = 42 } > ref.save > > You can''t assign the id in the attributes hash of the .new method > because ActiveRecord automatically protects the id from mass- > assignment. If you set the id in a block or in a separate statement, > then it will be used (unless it causes a conflict at the database > level because, for example, it has a unique index). > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.