diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 725a91a50..4ab86c32f 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -518,8 +518,8 @@ public function getAttributeWidth(Document $collection): int break; case Database::VAR_RELATIONSHIP: - // INT(11) - $total += 4; + // VARCHAR(255) + $total += Database::LENGTH_KEY * 4 + 2; break; case Database::VAR_DATETIME: diff --git a/src/Database/Database.php b/src/Database/Database.php index b8a66c16d..0350a658b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2169,22 +2169,6 @@ public function createRelationship( } } - if ( - $this->adapter->getLimitForAttributes() > 0 && - ($this->adapter->getCountOfAttributes($collection) >= $this->adapter->getLimitForAttributes() - || $this->adapter->getCountOfAttributes($relatedCollection) >= $this->adapter->getLimitForAttributes()) - ) { - throw new LimitException('Column limit reached. Cannot create new attribute.'); - } - - if ( - $this->adapter->getDocumentSizeLimit() > 0 && - ($this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() - || $this->adapter->getAttributeWidth($relatedCollection) >= $this->adapter->getDocumentSizeLimit()) - ) { - throw new LimitException('Row width limit reached. Cannot create new attribute.'); - } - $relationship = new Document([ '$id' => ID::custom($id), 'key' => $id, @@ -2217,6 +2201,9 @@ public function createRelationship( ], ]); + $this->checkAttribute($collection, $relationship); + $this->checkAttribute($relatedCollection, $twoWayRelationship); + $collection->setAttribute('attributes', $relationship, Document::SET_TYPE_APPEND); $relatedCollection->setAttribute('attributes', $twoWayRelationship, Document::SET_TYPE_APPEND); diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 3a80da72c..ad117acd7 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1364,6 +1364,59 @@ public function testSchemaAttributes(): void } } + public function testRowSizeToLarge(): void + { + if (static::getDatabase()->getAdapter()->getDocumentSizeLimit() === 0) { + $this->expectNotToPerformAssertions(); + return; + } + /** + * getDocumentSizeLimit = 65535 + * 65535 / 4 = 16383 MB4 + */ + $collection_1 = static::getDatabase()->createCollection('row_size_1'); + $collection_2 = static::getDatabase()->createCollection('row_size_2'); + + $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); + + try { + static::getDatabase()->createAttribute($collection_1->getId(), 'attr_2', Database::VAR_STRING, Database::LENGTH_KEY, true); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } + + /** + * Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit + */ + + try { + static::getDatabase()->createRelationship( + collection: $collection_2->getId(), + relatedCollection: $collection_1->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); + + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } + + try { + static::getDatabase()->createRelationship( + collection: $collection_1->getId(), + relatedCollection: $collection_2->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); + + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } + } + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes');