Rake db:seed 在本地工作,heroku rake db:seed 尝试将 null 插入 id 列

发布于 2024-10-23 13:21:38 字数 2436 浏览 8 评论 0原文

由于某种原因,当我尝试通过 heroku 执行此操作时,我的 db:seed 无法工作。当我在本地执行时效果很好。

paul@paul-laptop:~/rails_projects/foglift$ heroku rake db:seed
rake aborted!
PGError: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "metric_records" ("metric_id", "id", "created_at", "updated_at", "value", "school_id", "date") VALUES (1, NULL, '2011-03-18 20:26:39.792164', '2011-03-18 20:26:39.792164', 463866000.0, 1, '2009-01-01') RETURNING "id"

(See full trace by running task with --trace)
(in /app/5aca24ae-c5a7-4805-a3a1-b2632a5cf558/home)

我不确定为什么它试图将 null 放入 id 列中,这对我来说没有任何意义。

这是我的 seeds.rb 文件。

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).


  directory = "db/seed_data/"

# Pre-load All Schools
  School.delete_all
  path = File.join(directory, "schools.txt")
  open(path) do |schools|
    schools.read.each_line do |school|
      name, city, state, website, tuition, debt = school.chomp.split("|")
      School.create!(:name => name, :city => city, :state => state, :website => website, :current_tuition => tuition, :current_avg_debt => debt)
    end
  end

# Pre-load All Dashboards
  Dashboard.delete_all
  path = File.join(directory, "dashboards.txt")
  open(path) do |dashboards|
    dashboards.read.each_line do |dashboard|
     name, extra = dashboard.chomp.split("|")
     Dashboard.create!(:name => name)
    end
  end

# Pre-load All Metrics
  Metric.delete_all
  path = File.join(directory, "metrics.txt")
  open(path) do |metrics|
    metrics.read.each_line do |metric|
     name, dashboard_id, display, source = metric.chomp.split("|")
     Metric.create!(:name => name, :dashboard_id => dashboard_id, :display => display, :source => source)
    end
  end

# Pre-load All Metric Records
  MetricRecord.delete_all
  path = File.join(directory, "metric_records.txt")
  open(path) do |values|
    values.read.each_line do |value|
      metric_id, value, date_string, school_id = value.chomp.split("|")
      date_string = '01/01/' << date_string
      date = Date.strptime(date_string, "%d/%m/%Y") unless date_string.nil?
      MetricRecord.create!(:metric_id => metric_id, :value => value, :date => date, :school_id => school_id)
    end
  end

For some reason my db:seed is not working when I try to do it via heroku. It works fine when I do it locally.

paul@paul-laptop:~/rails_projects/foglift$ heroku rake db:seed
rake aborted!
PGError: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "metric_records" ("metric_id", "id", "created_at", "updated_at", "value", "school_id", "date") VALUES (1, NULL, '2011-03-18 20:26:39.792164', '2011-03-18 20:26:39.792164', 463866000.0, 1, '2009-01-01') RETURNING "id"

(See full trace by running task with --trace)
(in /app/5aca24ae-c5a7-4805-a3a1-b2632a5cf558/home)

I'm not sure why it's attempting to put a null into the id column, it doesn't make any sense to me.

Here's my seeds.rb file.

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).


  directory = "db/seed_data/"

# Pre-load All Schools
  School.delete_all
  path = File.join(directory, "schools.txt")
  open(path) do |schools|
    schools.read.each_line do |school|
      name, city, state, website, tuition, debt = school.chomp.split("|")
      School.create!(:name => name, :city => city, :state => state, :website => website, :current_tuition => tuition, :current_avg_debt => debt)
    end
  end

# Pre-load All Dashboards
  Dashboard.delete_all
  path = File.join(directory, "dashboards.txt")
  open(path) do |dashboards|
    dashboards.read.each_line do |dashboard|
     name, extra = dashboard.chomp.split("|")
     Dashboard.create!(:name => name)
    end
  end

# Pre-load All Metrics
  Metric.delete_all
  path = File.join(directory, "metrics.txt")
  open(path) do |metrics|
    metrics.read.each_line do |metric|
     name, dashboard_id, display, source = metric.chomp.split("|")
     Metric.create!(:name => name, :dashboard_id => dashboard_id, :display => display, :source => source)
    end
  end

# Pre-load All Metric Records
  MetricRecord.delete_all
  path = File.join(directory, "metric_records.txt")
  open(path) do |values|
    values.read.each_line do |value|
      metric_id, value, date_string, school_id = value.chomp.split("|")
      date_string = '01/01/' << date_string
      date = Date.strptime(date_string, "%d/%m/%Y") unless date_string.nil?
      MetricRecord.create!(:metric_id => metric_id, :value => value, :date => date, :school_id => school_id)
    end
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-10-30 13:21:39

您的数据库迁移是否有可能没有为 Heroku 数据库上的 metric_records ID 列创建序列?

您也许可以通过以下方法修复它:

CREATE SEQUENCE metric_records_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE metric_records_id_seq OWNED BY metric_records.id;

Is there a possibility that your DB migration did not create a sequence for your metric_records ID column on the Heroku database?

You might be able to fix it with the following:

CREATE SEQUENCE metric_records_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE metric_records_id_seq OWNED BY metric_records.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文