0

I have a migration like this:

   defmodule Q.Repo.Migrations.CreateTable.Service.Companies do

    def change do
      create table(:companies) do
      add(:status_id, references(:statuses, type: :varchar))
      add(:admin_id, references(:admins), null: false)
     end
   end
 end

Is there any way I can get the fields where null: false? like the admin_id in the above migration. There are some changeset functions

    action: action(),
    changes: %{optional(atom()) => term()},
    constraints: [constraint()],
    data: Ecto.Schema.t() | map() | nil,
    empty_values: term(),
    errors: [{atom(), error()}],
    filters: %{optional(atom()) => term()},
    params: %{optional(String.t()) => term()} | nil,
    prepare: [(t() -> t())],
    repo: atom() | nil,
    repo_opts: Keyword.t(),
    required: [atom()],
    types: nil | %{optional(atom()) => Ecto.Type.t()},
    valid?: boolean(),
    validations: [{atom(), term()}]

But I didn't find any related to this.

Thanks

script
  • 1,667
  • 1
  • 12
  • 24
  • if it is a admin_id, means it is referenced to admin table, it can never be null. Can you define more, with an example, may be – Zubair Nabi Feb 23 '18 at 10:25
  • @ZubairNabi I want to get that `admin_id` field name through some function. – script Feb 23 '18 at 10:29
  • is'nt that in your schema `:companies` [have you created the schema/model? or you have simply written the migration code only], btw, `create table(:comapnies) do` there is typo in `companies` – Zubair Nabi Feb 23 '18 at 10:34
  • @ZubairNabi . Thanks for pointing out the typo.this is the working schema migration file. I just want to get fields where null: false through __schema__ or changeset function by using the model name like company in my case – script Feb 23 '18 at 10:37
  • Schema doesn't have access to migrations. Maybe you can query the database engine at runtime for this. If you only need this to work with PostgreSQL, maybe this would help: https://stackoverflow.com/questions/5345380/finding-columns-that-are-not-null-in-postgresql – Dogbert Feb 23 '18 at 10:41
  • @Dogbert Thanks . Post it as an answer. – script Feb 23 '18 at 10:45

0 Answers0