dgraph-io/dgraph · error

while writing schema

Error message

while writing schema

What it means

dumpSchema iterates the SQL table guides, generates Dgraph schema predicate definitions via createDgraphSchema, and writes each line into m.schemaWriter (a bufio.Writer). The error is a pkg/errors.Wrapf of the underlying bufio/os write failure, so the original cause (disk full, closed writer, permissions) is chained in the wrapped error.

Source

Thrown at dgraph/cmd/migrate/dump.go:60

// invalid sql queries in cases where a table uses a reserved keyword as a
// column name
func escapeColumnNames(columnNames []string) []string {
	var escapedColNames []string
	for _, c := range columnNames {
		escapedColNames = append(escapedColNames, fmt.Sprintf("`"+"%s"+"`", c))
	}
	return escapedColNames
}

// dumpSchema generates the Dgraph schema based on m.tableGuides
// and sends the schema to m.schemaWriter
func (m *dumpMeta) dumpSchema() error {
	for table := range m.tableGuides {
		tableInfo := m.tableInfos[table]
		for _, index := range createDgraphSchema(tableInfo) {
			_, err := m.schemaWriter.WriteString(index)
			if err != nil {
				return errors.Wrapf(err, "while writing schema")
			}
		}
	}
	return m.schemaWriter.Flush()
}

// dumpTables goes through all the tables twice. In the first time it generates RDF entries for the
// column values. In the second time, it follows the foreign key constraints in SQL tables, and
// generate the corresponding Dgraph edges.
func (m *dumpMeta) dumpTables() error {
	for table := range m.tableInfos {
		fmt.Printf("Dumping table %s\n", table)
		if err := m.dumpTable(table); err != nil {
			return errors.Wrapf(err, "while dumping table %s", table)
		}
	}

	for table := range m.tableInfos {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Check free disk space on the output volume and rerun the migration.
  2. Verify the schema output path is writable and is a regular file, not a directory.
  3. Inspect the chained cause with %+v on the returned error to see the original write failure.
  4. Ensure no other process removed or truncated the schema file during the run.

Example fix

// before
_, err := m.schemaWriter.WriteString(index)
if err != nil {
	return errors.Wrapf(err, "while writing schema")
}
// after
if _, err := m.schemaWriter.WriteString(index); err != nil {
	return errors.Wrapf(err, "while writing schema (check disk space / file permissions)")
}
if err := m.schemaWriter.Flush(); err != nil {
	return errors.Wrapf(err, "while flushing schema writer")
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: before running migrate
if fi, err := os.Stat(outPath); err == nil && fi.IsDir() {
	return fmt.Errorf("schema output path %s is a directory", outPath)
}
if err := checkDiskSpace(filepath.Dir(outPath), 100<<20); err != nil {
	return err
}

Type guard

func isWriteErr(err error) bool {
	var pe *os.PathError
	return errors.As(err, &pe)
}

Try / catch

if err := migrate.Run(); err != nil {
	if strings.Contains(err.Error(), "while writing schema") {
		log.Printf("schema write failed: %+v", err) // full chain
	}
}

Prevention

When it happens

Trigger: m.schemaWriter.WriteString(index) fails while dumping schema for any table in m.tableGuides during dgraph migrate --schema/--data output generation; e.g. the output file was closed, the disk is full, or the writer's underlying file was deleted.

Common situations: Disk full on the node running the migration; output schema file path on a read-only or removed volume; running migrate with a schema file path that points at a directory; earlier code path (user declined overwrite at checkFile) leaving the file handle in a bad state.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/e9eb1a110441408a. Report an issue: GitHub.