I have attached a user schema (see below)
I would like to save a list of tunes for each user
How to do it?
For example
For email I used the EmailStr type
What to add in this line of code
Tune: Indexed()
from uuid import UUID, uuid4
from beanie import Document, Indexed
from pydantic import Field, EmailStr
class User(Document):
user_id: UUID = Field(default_factory=uuid4)
username: Indexed(str, unique=True)
email: Indexed(EmailStr, unique=True)
hashed_password: str
Tune: Indexed()
Related
I am developing a WebApp in flask and using flask-sqlalchemy to define my Models and store them in an SQLite database. In short, I have the following 3 models (A Porject can have multiple files, and each file can have multiple results):
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
files = db.relationship("File", backref="project", passive_deletes=True)
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
path = db.Column(db.String, nullable=False)
project_id = db.Column(
db.Integer,
db.ForeignKey("project.id", ondelete="CASCADE"),
nullable=False)
results = db.relationship("Result", backref="file", passive_deletes=True)
class Result(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String, nullable=False)
file_id = db.Column(
db.Integer,
db.ForeignKey("file.id", ondelete="CASCADE"),
nullable=False)
Now the problem is, that when I create a project, create and assign some files to the project, create some results and assign them to some files, and finally delete the project, the cascade deleting of the files and the results does not work. I found the following post and implemented the db.relationship attributes as suggested there, but the problem remains the same.
P.S.: here is a minimal working example, that shows the problem.
P.P.S.: Can somebody confirm, that the problem is reproducible. Not that it only happens on my computer/my environment.
Well, this answer goes in the category RTFM. As Gord Thompson "mentioned" with the link to the documentary of sqlalchemy, one needs to add the following lines somewhere reachable on flask app start:
from sqlalchemy.engine import Engine
from sqlalchemy import event
db = SQLAlchemy(app)
#event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
(now I am asking myself, why not a single word is written about this in the flask-sqlalchemy docu :-/ )
I have User entity that cointains attributes like: id, email, password...
Password attribute is a password hash, and I would like to remove it from GET result from item and collection operations.
Is there any annotation for this? ( I tried #ApiProperty (readable=false), but without success)
I suppose I can remove password from result through event subscriber but I am curious if there is any simple way how to achieve it.
Use the Groups in Entity
/**
* #ApiResource(attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* })
*/
class User {
/**
* #Groups("write")
* #ORM\Column(..........)
*/
private $password;
Doc: https://api-platform.com/docs/core/serialization#using-serialization-groups
I am trying to send a message to a topic with 4 partitions.And I want the message to go the partition as decided by the DefaultPartitioner(which uses the hash of the key)
kafkaTemplate.sendDefault(DefaultPartitioner(job.getId()),job.getId(),job);
I am not sure how to make the kafkaTemplate use the DefaultPartitioner to get the partition number.
Could someone please help me out with this.
Let's start from JavaDocs!
/**
* The default partitioning strategy:
* <ul>
* <li>If a partition is specified in the record, use it
* <li>If no partition is specified but a key is present choose a partition based on a hash of the key
* <li>If no partition or key is present choose a partition in a round-robin fashion
*/
public class DefaultPartitioner implements Partitioner {
/**
* Send the data to the default topic with the provided key and no partition.
* #param key the key.
* #param data The data.
* #return a Future for the {#link SendResult}.
*/
ListenableFuture<SendResult<K, V>> sendDefault(K key, V data);
So, if you would like to rely on the DefaultPartitioner, all you need is a key for the record. Therefore just use that particular KafkaTemplate method:
kafkaTemplate.sendDefault(job.getId(),job);
I'm using string(32) instead of integer for entity id field (it simulates guid type). But after creating database table from entity ids field type is set to varchar(32). I think it's not the best idea, guid always has length = 32 so char type will be more appropriate.
Is there a way to tell Doctrine to use char or the only way is to change it manually in database?
Although the althaus solution works fine I think this one should be the canonical way of doing it:
With annotations:
/**
* #Column(type="string", length=2, options={"fixed" = true})
*/
protected $country;
With YAML (example of both ID and non-ID field):
Your\Nice\Entity:
id:
id:
type: string
length: 32
options:
fixed: true
fields:
country:
type: string
length: 2
options:
fixed: true
options: Array of additional options:
fixed: Boolean value to determine if the specified length of a string column should be fixed or varying (applies only for string/binary column and might not be supported by all vendors).
Official documentation
You can tell Doctrine to use vendor specific field types:
/**
* #Column(type="string", columnDefinition="CHAR(2) NOT NULL")
*/
protected $country;
Source
I am new to symfony2 framework. I have a mysql db that I have prior to installing symfony. So I was able to generate schema based on the db.
I am trying to generate the CRUD controller for one of the tables. This is a user table and the primary key is UserId. However, when I run doctrine:generate:CRUD, I get the following error
The CRUD generator expects the entity object has a primary key field named "id" with a getId() method.
Do I have to have Id as the PK identifier? is that the best practice? All my PKs for the tables are defined by tablenameid.
I have the get method for the User entity class as getUserid().
How can I let the CRUD generator know that?
Thanks
I don't think it has to be 'id' as PK identifier. I think there might be some error in your entity.
/** #Id #Column(type="integer") #GeneratedValue */
private $userid;
YML File:
Bundle\BundleBundle\Entity\Example:
type: entity
table: Example
fields:
userid:
id: true
type: integer
generator:
strategy: AUTO