简体   繁体   English

MySQL Workbench-产生错误的SQL?

[英]MySQL Workbench - producing bad SQL?

I'm trying to make a table that has a "created" timestamp and an "updated" timestamp (and have MySQL insert values in there automatically for me). 我正在尝试制作一个具有“创建的”时间戳和“更新的”时间戳的表(并为我自动在其中插入MySQL值)。 It's doable, see: http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql/ . 这是可行的,请参见: http : //gusiev.com/2009/04/update-and-create-timestamps-with-mysql/

As a first step, I copied the example table from the reference site and modeled it in Workbench. 第一步,我从参考站点复制了示例表,并在Workbench中对其进行了建模。 I then did a forward engineer and tested the resulting SQL in phpMyAdmin. 然后,我做了一名前向工程师,并在phpMyAdmin中测试了生成的SQL。 Everything worked. 一切正常。 Here was the SQL: 这是SQL:

-- -----------------------------------------------------
-- Table `test_table`
-- -----------------------------------------------------

DROP TABLE IF EXISTS `test_table` ;

CREATE  TABLE IF NOT EXISTS `test_table` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `stamp_created` TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00' ,
  `stamp_updated` TIMESTAMP NULL DEFAULT now() on update now() ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;

After verifying that the idea works, I then implemented the concept in one of my actual tables. 在验证了该想法可行之后,我在一个实际表中实现了该想法。 The resulting SQL was this: 产生的SQL是这样的:

-- -----------------------------------------------------
-- Table `user_login`
-- -----------------------------------------------------

DROP TABLE IF EXISTS `user_login` ;

CREATE  TABLE IF NOT EXISTS `user_login` (
  `user_login_id` INT NOT NULL AUTO_INCREMENT ,
  `user_id` INT NOT NULL ,
  `hashed_password` CHAR(255) NULL ,
  `provider_id` CHAR(255) NULL ,
  `provider_name` CHAR(255) NULL ,
  `unverified_email_address` CHAR(255) NULL ,
  `verification_code_email_address` CHAR(255) NULL ,
  `verification_code_password_change` CHAR(255) NULL ,
  `verified_email_address` CHAR(255) NULL ,
  `stamp_created` TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00' ,
  `stamp_updated` TIMESTAMP NULL DEFAULT now() on update now() ,
  `stamp_deleted` TIMESTAMP NULL ,
  PRIMARY KEY (`user_login_id`) ,
  INDEX `user_login_user_id` (`user_id` ASC) ,
  UNIQUE INDEX `verified_email_address_UNIQUE` (`verified_email_address` ASC) ,
  UNIQUE INDEX `unverified_email_address_UNIQUE` (`unverified_email_address` ASC) ,
  CONSTRAINT `user_login_user_id`
    FOREIGN KEY (`user_id` )
    REFERENCES `user` (`user_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

When I execute the SQL, I get the following error: 执行SQL时,出现以下错误:

#1067 - Invalid default value for 'stamp_created'

Anyone see what's causing my problem? 有人看到造成我问题的原因吗?

stamp_created TIMESTAMP NULL DEFAULT 0,

or 要么

stamp_created TIMESTAMP NULL DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,

depending on your needs, however in your case logical should be: 根据您的需求,但是在您的情况下,逻辑应该是:

  stamp_created TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM