[Django] bulk_create로 pk 얻기

Dior·2023년 1월 16일
0
post-thumbnail

👨🏻‍💻 Env: Django, Mysql

✔ bulk_create()

objs = Entry.objects.bulk_create([
	Entry(headline='This is a test'),
    Entry(headline='This is only a test'),
])

objs.[0].id

아쉽지만 현재 Mysql에서 bulk_create를 사용해서 AutoField인 pk값을 가져올 수 없다.

If the model’s primary key is an AutoField, the primary key attribute can only be retrieved on certain databases (currently PostgreSQL, MariaDB 10.5+, and SQLite 3.35+). On other databases, it will not be set.

pk가 AutoField일 경우, PostgreSQL, MariaDB, SQLite에서는 얻을 수 있지만 다른 데이터베이스에서는 얻을 수 없습니다.

🙄 P.S.

save()를 통해서 pk는 가져올 수 있는데, bulk_create()로는 안된다.
Json Field를 사용할 때도 Mysql보다는 PostgreSQL이 더 유용하다.
Django랑 PostgreSQL이 핏이 잘 맞는 것 같다.

🙏참고자료

Stack Overflow
Django Docs

🙇🏻‍ 잘못된 정보는 댓글을 통해 알려주시면 감사하겠습니다.

profile
Focus on growth rather than material

0개의 댓글