ent/dialect/sql/schema: set nullability of columns on scan

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/106

Reviewed By: alexsn

Differential Revision: D17987588

fbshipit-source-id: f5f985d28bdd7379826dca35111c61f1c169d5c0
This commit is contained in:
Ariel Mashraki
2019-10-17 14:27:57 -07:00
committed by Facebook Github Bot
parent 0241a969b4
commit c3135d03a6

View File

@@ -93,15 +93,15 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
//
// Get PK and UNI columns of a table:
//
// SELECT a.attname AS COLUMN,
// SELECT a.attname AS column,
// format_type(a.atttypid, a.atttypmod) AS data_type,
// i.indisprimary AS PRIMARY,
// i.indisprimary AS primary,
// i.indisunique AS unique
// FROM pg_index i
// join pg_attribute a
// ON a.attrelid = i.indrelid
// AND a.attnum = ANY ( i.indkey )
// WHERE i.indrelid = '<TABLE>' :: regclass;
// FROM pg_index i
// join pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY (i.indkey)
// join pg_stat_user_tables t ON t.relid = i.indrelid
// WHERE t.schemaname = CURRENT_SCHEMA()
// AND i.indrelid = '<TABLE>' :: regclass;
//
// column | data_type | primary | unique
// --------+-----------+---------+--------
@@ -125,6 +125,9 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
if err := rows.Scan(&c.Name, &c.typ, &maxlen, &nullable, &defaults); err != nil {
return fmt.Errorf("scanning column description: %v", err)
}
if nullable.Valid {
c.Nullable = nullable.String == "YES"
}
switch c.typ {
case "boolean":
c.Type = field.TypeBool