Skip to content

insert_all with queries ignore field mapping and writability #4794

Description

@lukaszsamson

Elixir version

any

Database and Version

any

Ecto Versions

3.14

Database Adapter and Versions (postgrex, myxql, etc)

any

Current behavior

When Repo.insert_all/3 receives a source query using select: r, it builds the insert column list from the source schema instead of destination. This results in incorrect SQL in case of mapping and ignoring writable: false

Repro:
With those schemas

  defmodule InsertSelectSource do
    use Ecto.Schema

    @primary_key false
    schema "insert_select_source" do
      field :name, :string
    end
  end

  defmodule InsertSelectRenamed do
    use Ecto.Schema

    @primary_key false
    schema "insert_select_renamed" do
      field :name, :string, source: :renamed_name
    end
  end

  defmodule InsertSelectReadOnly do
    use Ecto.Schema

    @primary_key false
    schema "insert_select_read_only" do
      field :name, :string, writable: :never
    end
  end
      query = from s in InsertSelectSource, select: s
      TestRepo.insert_all(InsertSelectRenamed, query)

Errors with 42703 undefined_column on postgres

query = from s in InsertSelectSource, select: s
TestRepo.insert_all(InsertSelectReadOnly, query)

writes into filed marked as not writable

Expected behavior

destination schema respected

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions