1

我尝试在租户中以初始编号开始一个序列,但只有公共模式得到了这个。

在此处输入图像描述 看看我的迁移:

class CreateDisputes < ActiveRecord::Migration[5.0]
  def change
    create_table :disputes, id: :uuid do |t|
      ...
      t.integer    :code
      ...
    end

    execute %{
      CREATE SEQUENCE disputes_code_seq INCREMENT BY 1
        NO MINVALUE NO MAXVALUE
        START WITH 1001 CACHE 1
        OWNED BY disputes.code;

      ALTER TABLE ONLY disputes
        ALTER COLUMN code SET DEFAULT nextval('disputes_code_seq'::regclass);
    }

    ...
  end
end

谢谢!

4

1 回答 1

1

我不是apartement宝石专家。但是,apartment不是disputes_code_seq在租户的架构中创建。

解决方法是取消注释以下行config/initializers/apartment.rb

  # Apartment can be forced to use raw SQL dumps instead of schema.rb for creating new schemas.
  # Use this when you are using some extra features in PostgreSQL that can't be respresented in
  # schema.rb, like materialized views etc. (only applies with use_schemas set to true).
  # (Note: this option doesn't use db/structure.sql, it creates SQL dump by executing pg_dump)
  #
  config.use_sql = true

config.user_sql设置为,true公寓迁移将为租户创建序列。这是来自migrationrails console供参考的日志。

以下是迁移日志

ubuntu@ubuntu-xenial:~/devel/apartment/testseq$ rails db:migrate
== 20170224161015 CreateDisputes: migrating ===================================
-- create_table(:disputes)
   -> 0.0035s
-- execute("\n      CREATE SEQUENCE disputes_code_seq INCREMENT BY 1\n      NO MINVALUE NO MAXVALUE\n      START WITH 1001 CACHE 1\n      OWNED BY disputes.code;\n\n      ALTER TABLE ONLY disputes\n        ALTER COLUMN code SET DEFAULT nextval('disputes_code_seq'::regclass);\n    ")
   -> 0.0012s
== 20170224161015 CreateDisputes: migrated (0.0065s) ==========================

        [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:

          1. You may not have created any, in which case you can ignore this message
          2. You've run `apartment:migrate` directly without loading the Rails environment
            * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`

        Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.

以下是租户创建和添加行的日志disputes

irb(main):001:0> Apartment::Tenant.create('tenant2')
<output snipped for brevity>


irb(main):005:0> Apartment::Tenant.switch!('tenant2')
=> "\"tenant2\""
irb(main):006:0> d = Dispute.new
=> #<Dispute id: nil, code: nil, created_at: nil, updated_at: nil>
irb(main):007:0> d.save
   (0.2ms)  BEGIN
  SQL (0.6ms)  INSERT INTO "disputes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", 2017-02-25 03:09:49 UTC], ["updated_at", 2017-02-25 03:09:49 UTC]]
   (0.6ms)  COMMIT
=> true
irb(main):008:0> d.reload
  Dispute Load (0.3ms)  SELECT  "disputes".* FROM "disputes" WHERE "disputes"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
=> #<Dispute id: 1, code: 1001, created_at: "2017-02-25 03:09:49", updated_at: "2017-02-25 03:09:49">

正如您在以下日志中看到的,代码以序列号开头。

irb(main):009:0> d = Dispute.new
=> #<Dispute id: nil, code: nil, created_at: nil, updated_at: nil>
irb(main):010:0> d.save
   (0.3ms)  BEGIN
  SQL (0.6ms)  INSERT INTO "disputes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", 2017-02-25 03:11:13 UTC], ["updated_at", 2017-02-25 03:11:13 UTC]]
   (0.5ms)  COMMIT
=> true
irb(main):011:0> d.reload
  Dispute Load (0.5ms)  SELECT  "disputes".* FROM "disputes" WHERE "disputes"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
=> #<Dispute id: 2, code: 1002, created_at: "2017-02-25 03:11:13", updated_at: "2017-02-25 03:11:13">
于 2017-02-25T03:19:48.863 回答