findmypast-oss / mssql_ecto

Ecto adapter for Mssqlex
Apache License 2.0
49 stars 20 forks source link

How to handle capital letters in database column #30

Closed cdesch closed 6 years ago

cdesch commented 6 years ago

I have a database table with camel case column names.

 DemoCategoryKey
 Name
 CategoryNo
 Description

How do I handle the capital letters?

Here is what I used for a schema:


defmodule MsSqlTest.DemoCategory do
  use Ecto.Schema

  schema "DemoCategory" do
    field :name, :string     # Defaults to type :string
    field :category_no, :integer
    field :description, :string
  end
end

Expected Behavior

Query the record

Current Behavior

14:20:43.582 [debug] QUERY ERROR source="DemoCategory" db=6.3ms queue=0.1ms
SELECT D0."id", D0."name", D0."category_no", D0."description" FROM "DemoCategory" AS D0 []
** (Mssqlex.Error) [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'id'.[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'category_no'. | ODBC_CODE 42S22 | SQL_SERVER_CODE 207
    (ecto) lib/ecto/adapters/sql.ex:436: Ecto.Adapters.SQL.execute_and_cache/7
    (ecto) lib/ecto/repo/queryable.ex:130: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4

Your Environment

cdesch commented 6 years ago

lol. Simply fixed by using camel case.

defmodule MsSqlTest.DemoCategory do
  use Ecto.Schema

  @primary_key {:DemoCategoryKey, :uuid, autogenerate: false} 
  schema "DemoCategory" do
    #field :DemoCategoryKey, :uuid
    field :Name, :string     # Defaults to type :string
    field :CategoryNo, :integer
    field :Description, :string
  end
end