mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
doc: add schema inspection to getting-started page (#3510)
This commit is contained in:
@@ -8,6 +8,7 @@ import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
import AtlasMigrateDiff from './components/_atlas_migrate_diff.mdx';
|
||||
import AtlasMigrateApply from './components/_atlas_migrate_apply.mdx';
|
||||
import InstallationInstructions from './components/_installation_instructions.mdx';
|
||||
|
||||
**ent** is a simple, yet powerful entity framework for Go, that makes it easy to build
|
||||
and maintain applications with large data-models and sticks with the following principles:
|
||||
@@ -116,14 +117,14 @@ ent
|
||||
|
||||
## Create Your First Entity
|
||||
|
||||
To get started, create a new `ent.Client`.
|
||||
To get started, create a new `Client` to run schema migration and interact with your entities:
|
||||
|
||||
<Tabs
|
||||
defaultValue="sqlite"
|
||||
values={[
|
||||
{label: 'SQLite', value: 'sqlite'},
|
||||
{label: 'PostgreSQL', value: 'postgres'},
|
||||
{label: 'MySQL', value: 'mysql'},
|
||||
{label: 'MySQL (MariaDB)', value: 'mysql'},
|
||||
]}>
|
||||
<TabItem value="sqlite">
|
||||
|
||||
@@ -146,9 +147,11 @@ func main() {
|
||||
}
|
||||
defer client.Close()
|
||||
// Run the auto migration tool.
|
||||
// highlight-start
|
||||
if err := client.Schema.Create(context.Background()); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
// highlight-end
|
||||
}
|
||||
```
|
||||
|
||||
@@ -174,9 +177,11 @@ func main() {
|
||||
}
|
||||
defer client.Close()
|
||||
// Run the auto migration tool.
|
||||
// highlight-start
|
||||
if err := client.Schema.Create(context.Background()); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
// highlight-end
|
||||
}
|
||||
```
|
||||
|
||||
@@ -202,16 +207,19 @@ func main() {
|
||||
}
|
||||
defer client.Close()
|
||||
// Run the auto migration tool.
|
||||
// highlight-start
|
||||
if err := client.Schema.Create(context.Background()); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
// highlight-end
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
Now, we're ready to create our user. Let's call this function `CreateUser` for the sake of example:
|
||||
After running schema migration, we're ready to create our user. For the sake of this example, let's name this function
|
||||
_CreateUser_:
|
||||
|
||||
```go title="entdemo/start.go"
|
||||
func CreateUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
|
||||
@@ -442,6 +450,45 @@ func QueryCarUsers(ctx context.Context, a8m *ent.User) error {
|
||||
}
|
||||
```
|
||||
|
||||
## View the Generated SQL Schema
|
||||
|
||||
If you have reached this point, you have successfully executed the schema migration and created several entities in the
|
||||
database. To view the SQL schema generated by Ent for the database, install [Atlas](https://github.com/ariga/atlas)
|
||||
and run the following command:
|
||||
|
||||
#### Install Atlas
|
||||
|
||||
<InstallationInstructions />
|
||||
|
||||
#### Inspect The Ent Schema
|
||||
|
||||
```bash
|
||||
atlas schema inspect \
|
||||
-u "ent://ent/schema" \
|
||||
--dev-url "sqlite://file?mode=memory&_fk=1" \
|
||||
--format '{{ sql . " " }}'
|
||||
```
|
||||
|
||||
#### SQL Output
|
||||
|
||||
```sql
|
||||
-- Create "cars" table
|
||||
CREATE TABLE `cars` (
|
||||
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`model` text NOT NULL,
|
||||
`registered_at` datetime NOT NULL,
|
||||
`user_cars` integer NULL,
|
||||
CONSTRAINT `cars_users_cars` FOREIGN KEY (`user_cars`) REFERENCES `users` (`id`) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
-- Create "users" table
|
||||
CREATE TABLE `users` (
|
||||
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`age` integer NOT NULL,
|
||||
`name` text NOT NULL DEFAULT 'unknown'
|
||||
);
|
||||
```
|
||||
|
||||
## Create Your Second Edge
|
||||
|
||||
We'll continue our example by creating a M2M (many-to-many) relationship between users and groups.
|
||||
|
||||
Reference in New Issue
Block a user