<commit_before># FK 선언만 하는 경우 = 참조변수 있음(lazy 옵션 지정 안함 디폴트)

```python
p = Post.query.first()
```

```sql
SELECT post.id, post.title, post.body, post.pub_date, post.category_id 
FROM post
```

```python
p.category
```

```sql
SELECT category.id, category.name
FROM category 
WHERE category.id = ?
```

```python
c = Category.query.first()
```

```sql
SELECT category.id, category.name
FROM category
```

```python
c.posts
```

# Post 모델에서 category 선언 참조

## category = db.relationship('Category')

```python
p = Post.query.first()
```

```sql
SELECT post.id, post.title , post.body , post.pub_date, post.category_id 
FROM post
```

```python
p.category
```

```sql
SELECT category.id, category.name
FROM category
```

## category = db.relationship('Category', lazy='immediate')

- immediate, select, subquery 2.1.과 동일

## category = db.relationship('Category', lazy='joined')

```python
category = db.relationship('Category') 
```

```sql
SELECT post.id, post.title , post.body , post.pub_date, post.category_id, category.id, category.name
FROM post LEFT OUTER JOIN category ON category.id = post.category_id
```

## category = db.relationship('Category', lazy='dynamic')

```python
category = db.relationship('Category')
```

에러: On relationship Post.category, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.

# ```Category``` 모델에서 ```post``` 참조 선언

## posts = db.relationship('Post')

```python
c = Category.query.first()
```

```sql
SELECT category.id, category.name
FROM category
```<commit_msg>post 쪽 일대다 오류 수정<commit_after># lazy 옵션 지정 안함 디폴트

```python
p = Post.query.first()
```

```sql
SELECT post.id, post.title, post.body, post.pub_date, post.category_id 
FROM post
```

```python
p.category
```

```sql
SELECT category.id, category.name
FROM category 
WHERE category.id = ?
```

```python
c = Category.query.first()
```

```sql
SELECT category.id, category.name
FROM category
```

```python
c.posts
```

# Post 모델에서 category 선언 참조

## category = db.relationship('Category')

```python
p = Post.query.first()
```

```sql
SELECT post.id, post.title , post.body , post.pub_date, post.category_id 
FROM post
```

```python
p.category
```

```sql
SELECT category.id, category.name
FROM category
```

## category = db.relationship('Category', lazy='immediate')

- immediate, select, subquery 2.1.과 동일

## category = db.relationship('Category', lazy='joined')

```python
p = Post.query.first()
```

```sql
SELECT post.id AS post_id, post.title AS post_title, post.body AS post_body, post.pub_date AS post_pub_date, post.category_id AS post_category_id 
FROM post
```

```python
p.cateogry
```

```sql
SELECT category.id, category.name, t.post_category_id
FROM (SELECT DISTINCT post.category_id
FROM post) AS t JOIN category ON category.id = t.post_category_id ORDER BY t.post_category_id
```

## category = db.relationship('Category', lazy='dynamic')

```python
p.category
```

에러: On relationship Post.category, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.

# ```Category``` 모델에서 ```post``` 참조 선언

## posts = db.relationship('Post')

```python
c = Category.query.first()
```

```sql
SELECT category.id, category.name
FROM category
```
{'fraction_non_alphanumeric': 0.17006172839506173, 'fraction_numerical': 0.0012345679012345679, 'mean_word_length': 4.766903914590747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4868161', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1223, 'n_words': 302}