age icon indicating copy to clipboard operation
age copied to clipboard

how to create constraints

Open journeytosilius opened this issue 1 year ago • 2 comments

CREATE CONSTRAINT ON (person:Person) ASSERT person.id IS UNIQUE;

Is there a way to create constraints like this om AGE ? If there is, sorry since I can't find documentation about it, would appreciate if you can point out

journeytosilius avatar Mar 02 '24 21:03 journeytosilius

Hello, we are aware of this syntax, though is not yet implemented. Thanks for your interest.

dehowef avatar Mar 06 '24 19:03 dehowef

Hi, thanks. So how are constraints created ? Is this possible ? The thing is, I can't find anything in the docs

journeytosilius avatar Mar 07 '24 10:03 journeytosilius

@JourneyToSilius, have you figured out how to apply constraints in the graph?

I'm just now starting to exploring this.

NathanFrund avatar Jun 03 '24 21:06 NathanFrund

@NathanFrund here is a demo implementation of how I did it in Go:

func (s *store) CreateUniquePropertyFunction() error {

	queryFormat := `CREATE OR REPLACE FUNCTION graph.aggregated.get_vertex_address(properties agtype) 
	RETURNS agtype
	AS
	$BODY$
		SELECT graph.ag_catalog.agtype_access_operator($1, '"address"');
	$BODY$
	LANGUAGE sql
	IMMUTABLE;`

	_, err := s.AgePoolConnector.Pool.Exec(context.Background(), queryFormat)
	if err != nil {
		return fmt.Errorf("failed to execute query: %v", err)
	}
	return nil
}


func (s *store) CreateUniqueVertexConstraint() error {

	queryFormat := `CREATE UNIQUE INDEX idx_v2vertex_address_constraint ON graph.aggregated.v2vertex(get_vertex_address(properties));`
	_, err := s.AgePoolConnector.Pool.Exec(context.Background(), queryFormat)
	if err != nil {
		return handleQueryError(err)
	}

	return nil
}

journeytosilius avatar Jun 03 '24 23:06 journeytosilius