开发者

the column 'id' is not the primary_key ,how to reset the Relationship

开发者 https://www.devze.com 2023-03-03 15:27 出处:网络
I have a table form the second database,it is not through a migration class Dzhfeed < Dzxdb set_table_name \"pre_home_feed\"

I have a table form the second database,it is not through a migration

class Dzhfeed < Dzxdb

  set_table_name "pre_home_feed"

  set_primary_key :feedid

end

the table:

CREATE TABLE pre_home_feed (
  feedid int(10) unsigned NOT NULL auto_increment ,
  appid smallint(6) unsigned NOT NULL default '0' ,
  icon varchar(30) NOT NULL default '' ',
  uid mediumint(8) unsigned NOT NULL default '0' ,
  username varchar(15) NOT NULL default '' ',
  dateline int(10) unsigned NOT NULL default '0' ,
  friend tinyint(1) NOT NULL default '0',
  hash_template varchar(32) NOT NULL default '' ,
  hash_data varchar(32) NOT NULL default '' ,
  title_template text NOT NULL,
  title_data text NOT NULL,
  body_template text NOT NULL ,
  body_data text NOT NULL,
  body_general text NOT NULL ,
  image_1 varchar(255) NOT NULL default '' ,
  image_1_link varchar(255) NOT NULL default '',
  image_2 varchar(255) NOT NULL default '' ,
  image_2_link varchar(255) NOT NULL default '' ,
  image_3 varchar(255) NOT NULL default '' ,
  image_3_link varchar(255) NOT NULL default '' ,
  image_4 varchar(255) NOT NULL default '' ,
  image_4_link varchar(255) NOT NULL default '' ,
  target_ids text NOT NULL ,
  id mediumint(8) unsigned NOT NULL default '0' ,
  idtype varchar(15) NOT NULL default '' ,
  hot mediumint(8) unsigned NOT NULL default '0' ,
  PRIMARY KEY  (feedid),
  KEY uid (uid,dateline),
  KEY dateline (dateline),
  KEY hot (hot),
  KEY id (id,idtype)
) ;
开发者_如何转开发

but this table has another column named 'id' and not a primary_key

so ,when i want to create a new Dzhfeed ,I don't know how to set the column named id

my code is

feed = Dzhfeed.new(:appid => 0, :icon => 'doing', :uid => 1, :username => 'admin', :title_template => "xxxxxxxxxx", :body_template => '', :dateline => Time.now, :id => 0)

but it not work the error is

Mysql::Error: Column 'id' cannot be null: INSERT INTO `pre_home_feed` (`image_3`, `uid`, `id`, `dateline`, `title_template`, `idtype`, `image_1`, `username`, `body_template`, `image_1_link`, `image_3_link`, `friend`, `title_data`, `appid`, `body_data`, `image_2_link`, `hot`, `image_4_link`, `image_4`, `hash_template`, `body_general`, `icon`, `target_ids`, `hash_data`, `image_2`) VALUES ('', 1, NULL, 1304675043, 'xxxxxxxxxx', '', '', 'admin', '', '', '', 0, '', 0, '', '', 0, '', '', '', '', 'doing', '', '', '')


How did you create the table "pre_home_feed". If it is through a migration, it would have added a id column by default and set it as a primary key. In order not to add a 'id' column you have to say :id => false, :force => true in your table definition migration file and re-run the migration. ( Re-running a migration would first require a rollback, rake db:rollback. This command will undo the last migration run. )

The way you're setting the primary key is perfectly fine. You go about creating the objects in the standard way like this;

@feed = Dzhfeed.new( :feedid => :value .. )

I suggest you go through the Rails Guides.

Update:

Linking to a two different databases is a little different. Essentially, you need to use the establish_connection method defined in ActiveRecord::Base to do that:

establish_connection(
  :adapter  => "postgresql",
  :host     => "localhost",
  :username => "username",
  :password => "password",
  :database => "database_to_link_to"
)

Update:

According to this post here your problem should be solved using composite_primary_keys gem.

Essentially what you need to do is this:

Install the gem composite_primary_keys, and then

require 'composite_primary_keys'
class Dzhfeed < Dzxdb
  set_primary_keys :feedif    
end

Notice, it is set_primary_keys and not set_primary_key. This should solve your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消