简体   繁体   English

PG :: InFailedSqlTransaction创建初始租户

[英]PG::InFailedSqlTransaction on creating initial tenant

I'm converting my Rails project to multi-tenant and I'm using Apartment . 我正在将Rails项目转换为多租户,并且正在使用Apartment

I followed the directions on their README and the video they link to 我按照他们的自述文件及其链接的视频上的说明进行操作

My apartment.rb looks like this: 我的apartment.rb看起来像这样:

require 'apartment/elevators/subdomain'
Apartment.configure do |config|
  config.excluded_models = ['Project', 'User']
  config.tenant_names = lambda { Project.pluck :subdomain }
  config.use_schemas = true
  config.persistent_schemas = %w{ public }
end
Rails.application.config.middleware.use Apartment::Elevators::Subdomain

My Project model looks like this: 我的项目模型如下所示:

class Project < ApplicationRecord
  after_create :create_tenant

  private

  def create_tenant
    Apartment::Tenant.create(subdomain)
  end
end

I can run db:migrate and db:setup no problem. 我可以运行db:migrate和db:setup没问题。 It creates all my tables in the public schema then. 然后,它将在公共架构中创建我的所有表。

But, when I try to create a new Project, I get this (from rails c) 但是,当我尝试创建一个新项目时,我得到了这一点(来自c轨)

C:\code\vulnerability-history>rails c
Loading development environment (Rails 5.0.1)
irb(main):001:0> Project.create(name: 'foo', subdomain: 'bar')
   (0.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "public"."projects" ("name", "subdomain") VALUES ($1, $2) RETURNING "id"  [["name", "foo"], ["subdomain", "bar"]]
   (1.0ms)  CREATE SCHEMA "bar"
  SQL (1.0ms)  CREATE EXTENSION IF NOT EXISTS "plpgsql"
   (2.0ms)  DROP TABLE "commit_filepaths" CASCADE
   (14.0ms)  CREATE TABLE "commit_filepaths" ("id" serial primary key, "commit_id" integer NOT NULL, "filepath_id" integer NOT NULL, "total_churn" integer DEFAULT 0 NOT NULL)
   (1.0ms)  DROP TABLE "commits" CASCADE
   (11.0ms)  CREATE TABLE "commits" ("id" serial primary key, "commit_hash" character varying NOT NULL, "author_id" integer NOT NULL, "message" character varying NOT NULL, "date_created" timestamp NOT NULL, "notes" jsonb DEFAULT '{}' NOT NULL)
   (1.0ms)  DROP TABLE "developers" CASCADE
   (11.0ms)  CREATE TABLE "developers" ("id" serial primary key, "email" character varying NOT NULL)
   (1.5ms)  DROP TABLE "events" CASCADE
   (9.4ms)  CREATE TABLE "events" ("id" serial primary key, "detail_type" character varying NOT NULL, "detail_id" integer NOT NULL, "title_template" character varying DEFAULT ':title:' NOT NULL, "description_template" character varying DEFAULT ':description:' NOT NULL, "type_template" character varying DEFAULT ':type:' NOT NULL, "date_template" character varying DEFAULT ':date:' NOT NULL, "style_id" integer NOT NULL)
   (1.0ms)  DROP TABLE "filepaths" CASCADE
   (11.0ms)  CREATE TABLE "filepaths" ("id" serial primary key, "filepath" character varying NOT NULL, "notes" jsonb DEFAULT '{}' NOT NULL)
   (1.0ms)  DROP TABLE "fixes" CASCADE
   (11.0ms)  CREATE TABLE "fixes" ("id" serial primary key, "commit_id" integer NOT NULL, "vulnerability_id" integer NOT NULL, "notes" jsonb DEFAULT '{}' NOT NULL)
   (1.0ms)  DROP TABLE "projects" CASCADE
   (13.0ms)  CREATE TABLE "projects" ("id" serial primary key, "name" character varying NOT NULL, "subdomain" character varying NOT NULL)
   (1.0ms)  DROP TABLE "releases" CASCADE
   (10.6ms)  CREATE TABLE "releases" ("id" serial primary key, "number" integer NOT NULL, "date_released" timestamp NOT NULL, "project" character varying NOT NULL, "notes" jsonb NOT NULL)
   (1.0ms)  DROP TABLE "styles" CASCADE
   (11.3ms)  CREATE TABLE "styles" ("id" serial primary key, "name" character varying NOT NULL, "color" character varying DEFAULT '#ffffff' NOT NULL, "icon" character varying DEFAULT 'stars' NOT NULL)
   (1.0ms)  DROP TABLE "users" CASCADE
   (11.4ms)  CREATE TABLE "users" ("id" serial primary key, "username" character varying NOT NULL)
   (1.0ms)  DROP TABLE "vccs" CASCADE
   (10.0ms)  CREATE TABLE "vccs" ("id" serial primary key, "commit_id" integer NOT NULL, "vulnerability_id" integer NOT NULL, "notes" jsonb DEFAULT '{}' NOT NULL)
   (1.0ms)  DROP TABLE "vulnerabilities" CASCADE
   (12.3ms)  CREATE TABLE "vulnerabilities" ("id" serial primary key, "cve" character varying NOT NULL, "announced" timestamp NOT NULL, "description" character varying DEFAULT '' NOT NULL, "notes" jsonb DEFAULT '{}' NOT NULL)
   (1.0ms)  SELECT version FROM "schema_migrations"
  ActiveRecord::InternalMetadata Load (1.0ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]
   (0.0ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: SET search_path TO "public", "public"
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in `block in log'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:315:in `schema_search_path='
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/postgresql_adapter.rb:40:in `reset'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:109:in `rescue in ensure in switch'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:109:in `ensure in switch'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:109:in `switch'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:26:in `block in create'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:97:in `__run_callbacks__'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:750:in `_run_create_callbacks'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:90:in `run_callbacks'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/apartment-1.2.0/lib/apartment/adapters/abstract_adapter.rb:23:in `create'
... 22 levels...
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:211:in `transaction'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:319:in `block in save'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:334:in `rollback_active_record_state!'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:318:in `save'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/suppressor.rb:41:in `save'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/persistence.rb:34:in `create'
        from (irb):1
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
        from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

I did notice this issue with a similar stacktrace, but the workaround doesn't make sense to me. 我也注意到这个问题有一个类似的堆栈跟踪,但解决方法没有道理给我。

I'm using: Ruby 2.3.1 (2016-04-26 patchlevel 112) [x64-mingw32] apartment 1.2.0 Rails 5.0.1 我正在使用:Ruby 2.3.1(2016-04-26补丁程序级别112)[x64-mingw32]公寓1.2.0 Rails 5.0.1

If the log is to be trusted, it appears to be failing on: 如果该日志值得信任,则似乎失败:

SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", :environment], ["LIMIT", 1]]

Does your db/schema.rb contain this table? 您的db / schema.rb是否包含此表? It's a new system table in Rails 5. 这是Rails 5中的新系统表。

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

相关问题 调试 PG::InFailedSqlTransaction - Debug PG::InFailedSqlTransaction ActiveRecord::StatementInvalid: PG InFailedSqlTransaction - ActiveRecord::StatementInvalid: PG InFailedSqlTransaction 后挂钩错误,来自Rspec的PG :: InFailedSqlTransaction - Error in an after hook, PG::InFailedSqlTransaction from Rspec Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: - Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: 添加新列时如何避免PG :: InFailedSqlTransaction? - How can I avoid PG::InFailedSqlTransaction when adding new columns? Rails 4:使用 RSpec 测试 rake 任务时的 PG::InFailedSqlTransaction - Rails 4: PG::InFailedSqlTransaction when testing a rake task with RSpec PG::InFailedSqlTransaction: 错误: 当前事务被中止,命令被忽略直到事务块结束 - PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block Apartment and Devise不创建租户 - Apartment and Devise not creating tenant 创建新的租户公寓宝石 - 总是重启? - Creating New Tenant Apartment Gem - Always restart? rails-活动管理员-PG :: InvalidColumnReference:错误:SELECT DISTINCT ON表达式必须与初始ORDER BY表达式匹配 - rails - active admin - PG::InvalidColumnReference: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM