mysql默认值错误

时间:2012-10-05 18:04:43

标签: mysql sinatra

我正在测试使用mysql数据库的链接缩短器Sinatra应用程序。当我输入要缩短的链接时,我收到了这个mysql错误

 (mysql_errno=1364, sql_state=HY000) Field 'link_identifier' doesn't have a default value Query: INSERT INTO `urls` (`original`) VALUES ('http://pryrepl.org/screencasts.html')

我假设这意味着字段link_identifier应该有一个默认值

网址和访问表都有link_identifier字段,但链接表没有。但是,在创建表的代码中,复制如下,它是具有`property:identifier'的Link类。有谁知道我会如何解决这个问题?

mysql> show columns from urls;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| original        | varchar(255)     | YES  |     | NULL    |                |
| link_identifier | varchar(50)      | NO   | MUL | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
3 rows in set (0.12 sec)

mysql> show columns from visits
    -> ;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| created_at      | datetime         | YES  |     | NULL    |                |
| ip              | varchar(39)      | YES  |     | NULL    |                |
| country         | varchar(50)      | YES  |     | NULL    |                |
| link_identifier | varchar(50)      | NO   | MUL | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
5 rows in set (0.08 sec)

mysql> show columns from links
    -> ;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| identifier | varchar(50) | NO   | PRI | NULL    |       |
| created_at | datetime    | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

代码

class Url
  include DataMapper::Resource
  property  :id,          Serial
  property  :original,    String, :length => 255   
  belongs_to  :link
end
....

class Link
  include DataMapper::Resource
  property  :identifier,  String, :key => true
  property  :created_at,  DateTime 
  has 1, :url
  has n, :visits

...

class Visit
  include DataMapper::Resource

  property  :id,          Serial
  property  :created_at,  DateTime
  property  :ip,          IPAddress
  property  :country,     String
  belongs_to  :link

  after :create, :set_country

....

更新 - 源代码似乎已经有办法创建一个链接标识符来满足mysql的要求,但它似乎没有工作

def self.create_link(original)
    url = Url.create(:original => original)
    if Link.first(:identifier => url.id.to_s(36)).nil? or !DIRTY_WORDS.include? url.id.to_s(36)
      link = Link.new(:identifier => url.id.to_s(36))
      link.url = url
      link.save 
      return link     
    else
      create_link(original)
    end    
  end
end

2 个答案:

答案 0 :(得分:0)

错误表示您没有在insert语句中为link_identifier设置值。 link_identifier不能由表定义null

您需要为link_identifier指定一个值。实施例

INSERT INTO `urls` (`original`, `link_identifier`) 
VALUES ('http://pryrepl.org/screencasts.html', 'value_for_link_identifier')

答案 1 :(得分:0)

发送查询时

 INSERT INTO `urls` (`original`) VALUES ('http://pryrepl.org/screencasts.html')

未设置link_identifier的值。

您将网址表定义为

mysql> show columns from urls;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| original        | varchar(255)     | YES  |     | NULL    |                |
| link_identifier | varchar(50)      | NO   | MUL | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
3 rows in set (0.12 sec)

link_identifier的默认值为NULL(或未设置),但由于该值不能为NULL,因此mysql不知道要将值设置为什么。

将mysql查询更改为

 INSERT INTO `urls` (`original`, `link_identifier`) VALUES ('http://pryrepl.org/screencasts.html', '<your identifier here>')