jihantoro

jihantoro

Ecto Schema Generator

hi, i have a lot of table in my db, is there any tool that automaticallly generate schema based on table column (with column types, etc)

something like, i just need to define credentials and database, and the tool will generate schema for all table

Most Liked

mikemccall

mikemccall

I was looking for a tool like this too. Something that worked much like django’s inspect.db. I ended up writing some one off scripts and using phoenix’s mix phx.gen.schema.

You can obtain information about your database using something like:

select *
from INFORMATION_SCHEMA.TABLES
where table_schema = 'database';

and

select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'table_name';

I still ended up doing plenty manual work but this did save a lot of time. It was a one off thing it didn’t bother me too much. I don’t know how much that helps but it’s another option.

Eiji

Eiji

Firstly it uses template. I think that much better is simple macro here. It’s harder to read its code. Secondly it supports really low number of types. I can see only DateTime, but no Date and Time. There is no array support as well as json/jsonb support. Thirdly it generates field with float type for decimal database type. As said of course such tool does not support lots of other things which @jihantoro would need to change anyway. There could be a good scenic tool which asks what to do in special cases, but such generator is too limited and there is no simple solution for all edge cases. Of course we could say that’s enough at start, but if you have really big number of different tables then ensuring that decimal have proper declaration is not so easy too while project could still compile which causes lots of confusion. For example think how to properly support enum inside json/jsonb column.

benperiton

benperiton

Possibly helpful posts;

Last Post!

OvermindDL1

OvermindDL1

For note, I wouldn’t really consider such a tool very useful, schema’s should match the query patterns, which may not necessarily match the tables. In my case I do have a schema ‘near’ matching each table, but I also have a lot more schemas for all the return data from the queries to keep everything typed and clean. (Except in a few cases where I return maps/lists/tuples/whatever for dynamic or pipelining reasons.)

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement