错误与ActiveRecords [英] Bug with ActiveRecords

查看:148
本文介绍了错误与ActiveRecords的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法删除有关从ActiveRecord的缓存表和列的信息。 我使用的ActiveRecord为Ruby没有Rails的。

 要求'active_record
的ActiveRecord :: Base.establish_connection(
    :适配器=> mysql2
    :数据库=> #
    :密码=> #
)
类Person<的ActiveRecord :: Base的
  belongs_to的:人民
结束
在这里输入code
一类人<的ActiveRecord :: Base的
  belongs_to的:人民
结束

类人与LT;的ActiveRecord :: Base的
  的has_many:人
结束


Person.new

ActiveRecord的:: StatementInvalid:Mysql2 ::错误:表'vkusno.people不存在:显示全FIELDS FROM`people`

Persons.new
ActiveRecord的:: StatementInvalid:Mysql2 ::错误:表'vkusno.persons不存在:显示全FIELDS FROM`persons`
 

不过,我尝试连接到数据库没有一些表和列。

在我的数据库中,人,人民,人,人进行了表,但我放弃我所有的表,并重新启动我的服务器几次。

如果我我的数据库更改为sqlite的,我得到一些不存在的表,其中我的工作和下降的表了。

我怎么能修好吗?

UPD

 的ActiveRecord :: Base.logger = Logger.new(标准输出)

类AddFirst< ActiveRecord的::迁移
  可变形点焊了
    CREATE_TABLE:人做了| T |
      t.integer:IDD
      t.string:名称
      t.string:HREF
      t.string:性别
      t.string:国家
      t.string:城市
      t.boolean:can_message
      t.boolean:can_wall
      t.string:照片
      t.boolean:is_friend
      t.boolean:is_client
    结束
    CREATE_TABLE:人们做| x |
      x.integer:id_general
      x.string:描述
    结束
  结束
  DEF下降
    drop_table:人
    drop_table:人
  结束
  DEF键
    add_column:人,:peoples_id,整数
    add_index:人,:peoples_id
  结束
结束

> AddFirst.new.up
 -  CREATE_TABLE(:人)
CREATE TABLE`persons`(`ID` INT(11)DEFAULT NULL AUTO_INCREMENT PRIMARY KEY,`idd` INT(11),`name`为varchar(255),`href`为varchar(255),`sex`为varchar(255) ,`country`为varchar(255),`city`为varchar(255),`can_message` TINYINT(1)`can_wall` TINYINT(1)`photo`为varchar(255),`is_friend` TINYINT(1),` is_client` TINYINT(1))ENGINE = InnoDB的

 -  CREATE_TABLE(:人)
CREATE TABLE`people`(`ID` INT(11)DEFAULT NULL AUTO_INCREMENT PRIMARY KEY,`id_general` INT(11),`description`为varchar(255))ENGINE = InnoDB的
=> {}

AddFirst.new.keys
 -  add_column(:人,:peoples_id,整数)
ALTER TABLE`persons`加上`peoples_id` INT(11)
 -  add_index(:人,:peoples_id)
CREATE INDEX`index_persons_on_peoples_id`开`persons`(`peoples_id`)
=>零



> Person.new
=> #<人物ID:无,id_general:无,说明:无>
> Persons.new
=> #<人ID:无,IDD:无,名:无,HREF:无,性别:无,国:无,城:无,can_message:无,can_wall:无,照片:无,is_friend:无,is_client:零>
> People.new
=> #<人ID:无,id_general:无,说明:无>
 

解决方案

了解如何类名映射到表名

您需要了解如何的ActiveRecord 决定的表名的典范。

如果你有一个类,默认的表名称将是。 AR通过调用类名的 tableize 的方法,这样做的:

 'Person'.tableize#=> 人
 

  Person.name.tableize#=> 人
 

这意味着,在类,您应该创建表(除非你想覆盖默认)。确保你的拼写正确; 人民没有的工作。

这也意味着你应该的没有的有另一个类人民,因为默认的表名也将是

 'People'.tableize#=> 人
 

会有冲突,或者在最好的,你会迷惑自己。

这不是一个好主意,用作为表名,除非你重写默认。这是因为AR不会产生作为类的表名

我强烈建议您阅读和理解 ActiveRecord的基础知识指南尝试之前别的。

造型现实世界中

我也不知道为什么你想要属于:人民

你能描述真实世界的情况下,您正在尝试重新present?

例如:

  • 一个人有很多书
  • 一个人属于一个组织。

I can't delete information about tables and columns from ActiveRecord's cache. I'm using ActiveRecord for Ruby without Rails.

require 'active_record'
ActiveRecord::Base.establish_connection(
    :adapter => "mysql2",
    :database  => #
    :password => #
)   
class Person < ActiveRecord::Base
  belongs_to :peoples
end
enter code here
class Persons < ActiveRecord::Base
  belongs_to :peoples
end

class People < ActiveRecord::Base
  has_many :persons
end


Person.new

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'vkusno.people' doesn't exist: SHOW FULL FIELDS FROM `people`'

Persons.new
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'vkusno.persons' doesn't exist: SHOW FULL FIELDS FROM `persons`'

But I try to connect to a Database without some table and columns.

Before I had a table in the database, "People, Peoples, Person, Persons", but I drop all my tables and restarted my server a few times.

If I change my database to sqlite, I get some don't exist tables, which it I'm working and drop that tables too.

How I can repair it?

UPD

ActiveRecord::Base.logger = Logger.new(STDOUT)

class AddFirst < ActiveRecord::Migration
  def up
    create_table :persons do |t|
      t.integer :idd
      t.string :name
      t.string :href
      t.string :sex
      t.string :country
      t.string :city
      t.boolean :can_message
      t.boolean :can_wall
      t.string :photo
      t.boolean :is_friend
      t.boolean :is_client
    end
    create_table :people do |x|
      x.integer :id_general
      x.string :description
    end
  end
  def down
    drop_table :persons
    drop_table :people
  end
  def keys
    add_column :persons, :peoples_id, :integer
    add_index :persons, :peoples_id
  end
end

> AddFirst.new.up
-- create_table(:persons)
CREATE TABLE `persons` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `idd` int(11), `name` varchar(255), `href` varchar(255), `sex` varchar(255), `country` varchar(255), `city` varchar(255), `can_message` tinyint(1), `can_wall` tinyint(1), `photo` varchar(255), `is_friend` tinyint(1), `is_client` tinyint(1)) ENGINE=InnoDB

-- create_table(:people)
CREATE TABLE `people` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `id_general` int(11), `description` varchar(255)) ENGINE=InnoDB
=> {}

AddFirst.new.keys
-- add_column(:persons, :peoples_id, :integer)
ALTER TABLE `persons` ADD `peoples_id` int(11)
-- add_index(:persons, :peoples_id)
CREATE INDEX `index_persons_on_peoples_id` ON `persons` (`peoples_id`) 
=> nil



> Person.new
=> #<Person id: nil, id_general: nil, description: nil>
> Persons.new
=> #<Persons id: nil, idd: nil, name: nil, href: nil, sex: nil, country: nil, city: nil, can_message: nil, can_wall: nil, photo: nil, is_friend: nil, is_client: nil>
> People.new
=> #<People id: nil, id_general: nil, description: nil>

解决方案

Understanding how class names are mapped to table names

You need to understand how ActiveRecord decides the table name for a model.

If you have a class Person, the default table name will be people. AR does this by calling the tableize method on the class name, like this:

'Person'.tableize  # => "people"

or

Person.name.tableize   # => "people"

That means, for the Person class, you should create the people table (unless you want to override the default). Make sure you get the spelling correct; peoples will not work.

That also means you should not have another class People because the default table name will also be people:

'People'.tableize  #=> "people"

There will be a clash, or at best, you'll confuse yourself.

It's not a good idea to use persons as the table name, unless you're overriding the default. That's because AR will not generate persons as the table name for class Person.

I strongly encourage you to read and understand the ActiveRecord basics guide before you try anything else.

Modeling the real world

I'm also not sure why you want Person to belong to :peoples.

Can you describe the real-world scenario you're trying to represent?

Examples:

  • a person has many books
  • a person belongs to an organization.

这篇关于错误与ActiveRecords的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆