laravel created_at和updated_at是否相反?

时间:2016-01-01 09:03:21

标签: sql laravel laravel-5 eloquent laravel-5.2

Laravel Framework版本5.2.5

当我更新记录时,update_at未更改,但created_at已更改为当前时间戳。

这是对的吗?

MariaDB [moon]> show columns from users;
+----------------+------------------+------+-----+---------------------+-----------------------------+
| Field          | Type             | Null | Key | Default             | Extra                       |
+----------------+------------------+------+-----+---------------------+-----------------------------+
| id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment              |
| name           | varchar(255)     | NO   |     | NULL                |                             |
| email          | varchar(255)     | NO   | UNI | NULL                |                             |
| password       | varchar(60)      | NO   |     | NULL                |                             |
| remember_token | varchar(100)     | YES  |     | NULL                |                             |
| created_at     | timestamp        | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                             |
+----------------+------------------+------+-----+---------------------+-----------------------------+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.jena.graph.Triple ;
import org.apache.jena.riot.RDFDataMgr ;
import org.apache.jena.riot.lang.PipedRDFIterator;
import org.apache.jena.riot.lang.PipedRDFStream;
import org.apache.jena.riot.lang.PipedTriplesStream;

public class ReadingTTL
{
    public static void main(String... argv) {
        final String filename = "yagoTransitiveType2.ttl";

        // Create a PipedRDFStream to accept input and a PipedRDFIterator to
        // consume it
        // You can optionally supply a buffer size here for the
        // PipedRDFIterator, see the documentation for details about recommended
        // buffer sizes
        PipedRDFIterator<Triple> iter = new PipedRDFIterator<>();
        final PipedRDFStream<Triple> inputStream = new PipedTriplesStream(iter);

        // PipedRDFStream and PipedRDFIterator need to be on different threads
        ExecutorService executor = Executors.newSingleThreadExecutor();

        // Create a runnable for our parser thread
        Runnable parser = new Runnable() {

            @Override
            public void run() {
                // Call the parsing process.
                RDFDataMgr.parse(inputStream, filename);
            }
        };

        // Start the parser on another thread
        executor.submit(parser);

        // We will consume the input on the main thread here

        // We can now iterate over data as it is parsed, parsing only runs as
        // far ahead of our consumption as the buffer size allows
        while (iter.hasNext()) {
            Triple next = iter.next();
            // Do something with each triple
            System.out.println("Subject:  "+next.getSubject());
            System.out.println("Object:  "+next.getObject());
            System.out.println("Predicate:  "+next.getPredicate());
            System.out.println("\n");
        }
    }

}

3 个答案:

答案 0 :(得分:2)

很多人最近遇到过这个问题,你可以在这里阅读关于Github的讨论:https://github.com/laravel/framework/issues/11518

  

MySQL 5.7不再允许0000-00-00作为有效的时间戳   严格模式打开(默认情况下)。所以要么使用   ->nullableTimestamps()->timestamp()->useCurrent()

您可以通过更改此内容来解决此问题:

$table->timestamps();

以下任一选项之一:

// Option 1:
$table->nullableTimestamps();

// Option 2:
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();

此外,在此MySQL页面上:https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

  

或者,如果禁用explicit_defaults_for_timestamp(默认值),请执行以下任一操作:

     

使用DEFAULT子句定义列,该子句指定常量默认值。

     

指定NULL属性。这也会导致列允许NULL值,这意味着您无法通过将列设置为NULL来分配当前时间戳。分配NULL会将列设置为NULL。

在任何一种情况下,上述解决方案都应该解决您的问题。

答案 1 :(得分:0)

查看数据库结构,on update CURRENT_TIMESTAMP列的问题为created_at。它不应该是这样的。在查看迁移时,此迁移不太可能自动设置,因此可能已在MySQL中手动设置,因此您应删除on update CURRENT_TIMESTAMP列的created_at

当您使用Eloquent updated_at列更新模型时,应该更新没有问题(也在当前数据库模式中) - 如果不是您可能不使用Eloquent,那么它将不会自动更新。如果您不确定,请显示您的代码如何更新此记录

答案 2 :(得分:0)

大家好,我的名字是来自伊朗的arash laghaei 我解决了这个问题 请这样做: 供应商/ laravel /框架/ SRS /照亮/数据库/模式/ blueprint.php 行:792

 public function timestamps()
{
    $this->timestamp('updated_at');

    $this->timestamp('created_at')->useCurrent();

}

请完全写下来。 并解决了这个问题。